【问题标题】:I receive NULL values when trying to send Ajax jQuery parameters in POST request - MVC 5尝试在 POST 请求中发送 Ajax jQuery 参数时收到 NULL 值 - MVC 5
【发布时间】:2018-11-28 16:15:13
【问题描述】:

我正在尝试将subscriberemail 参数发送到控制器中的操作结果,但是当我尝试在帖子中发送 ajax jquery 参数时收到空值。

    $("#button-subscribe-newsletter").click(function () {
        debugger
        if ($('#subscribe_form_email').val().length > 0)
        {
            if (isEmail($('#subscribe_form_email').val())) {

                var obj = {};
                obj["subscriberemail"] = $("#subscribe-form-email").val();

                $.ajax({
                    dataType: "json",
                    url: '/CreateSubscribe/Create',
                    type: 'post',
                    data: obj,
                    success: function (d) {
                        if (d.val == true) {
                            $("#subscribenewsletterform").hide();
                            $("#Success-Message").show();
                            $("#Failed-Message").hide();
                            $("#Null-Message").hide();
                        }
                        else {
                            $("#Failed-Message").show();
                        }
                    }
                });
            } else {
                $("#Null-Message").show();
            }
              
        } else {
            $("#Null-Message").show();
        }

        });
   <input type="email" id="subscribe_form_email" name="subscribe-form-email" class="bg-transparent text-small no-margin border-color-medium-dark-gray" placeholder="<%= LocalizationHelpers.Resource(null,"Enteryouremail", "ContentResources") %>">
                                    <button id="button-subscribe-newsletter"  type="submit" class="btn btn-arrow-small position-absolute border-color-medium-dark-gray">
                                        <i class="fa fa-caret-right no-margin-left"></i></button>

这是控制器

 public class CreateSubscribeController : Controller
{
    // GET: CreateSubscribe
    public ActionResult Index()
    {
        return View();
    }


    public ActionResult Create(string subscriberemail)
    {

        var keys = new Dictionary<string, string>() {
            { "Email",subscriberemail }
        };

        if (SitefinityWebApp.Helpers.SitefinityHelper.CreateSubscriberAndAddToMailingList(MailList.Maillist, subscriberemail))
        {
            SitefinityWebApp.Helpers.SitefinityHelper.SendEmailByTemplate(EmailTemplates.SubscribeEmail, subscriberemail, keys);
            return Json(new { val = true }, JsonRequestBehavior.AllowGet);
        }
        else
        {
            return Json(new { val = false }, JsonRequestBehavior.AllowGet);
        }
    }
}

任何建议

【问题讨论】:

  • 错字。 id 有下划线。选择器有破折号。

标签: jquery ajax asp.net-mvc asp.net-ajax


【解决方案1】:

在您的 Jquery 中更改您的代码,如下所示

obj["subscriberemail"] = $("#subscribe_form_email").val();

【讨论】:

  • 相反,您可以直接将变量作为数据发送:ajax 中的订阅者电子邮件。
  • 鉴于问题只是选择器中的拼写错误,适用于因拼写错误关闭,未回答。
猜你喜欢
  • 2017-02-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-07-25
  • 1970-01-01
相关资源
最近更新 更多