简体   繁体   English

无法通过Ajax到达我的控制器

[英]Cannot reach my controller via Ajax

I'm trying to use Ajax for the first time. 我正在尝试第一次使用Ajax。

The issue I am getting an error: uncaught referenceError: Data is not defined which I can see in Firebug 我遇到错误的问题: 未捕获的referenceError:未定义数据 ,我可以在Firebug中看到

 <script>
        $(function () {

            var form = $("#ringMeBack");

            form.submit(function () {

                var emailData = {
                    RingMeBackName: $("#ringMeBackName").val(),
                    RingMeBackNumber: $("#ringMeBackNumber").val()
                    //, RingMeBackTime: $("#ringMeBackTime").val()
                };

                $.ajax({
                    url: "/Home/RingMeBack",
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "text",
                    data: JSON.stringify(emailData),   //THIS IS THE ERROR
                    success: function (response) {
                        alert(response);
                    },
                    error: function () {
                        alert("There was an error... please try again.");
                    }
                });

                return false;
            });
        });
</script>

 @using (Html.BeginForm("RingMeBack", "Home", FormMethod.Post, new { id = "ringMeBack" }))
        {
            <p>
                <input type="text" id="ringMeBackName" title="Optional, but it's nice to know who we are talking to." placeholder="Your name" />
            </p>
            <p>
                <input type="text" id="ringMeBackNumber" title="We need to know the best number to call you on." placeholder="Your phone number" />
            </p>
            <p>
                When is a good time to call you?
            </p>
            <p>
                <select title="When is the best time to call you?" id="ringMeBackTime">
                    <!--don't show if it's after 2pm -->
                    @if (DateTime.Now.Hour < 14)
                    {
                        <option value="asap">Today, as soon as possible</option>
                    }
                    <option value="am" id="ringBackMorning" /><!--values created in Javascript-->
                    <option value="pm" id="ringBackAfternoon" /><!--values created in Javascript-->
                </select>
            </p>

            <p>
                <input type="submit" value="Request call back" title="Send and we'll call you back" class="redButton"/>
            </p>

            @ViewBag.Status
        }

If I update the data: JSON.stringify(emailData) to data: emailData.serialize() , it makes no difference. 如果我将data: JSON.stringify(emailData)更新为data: emailData.serialize() ,则没有任何区别。

As per the link I cite, if I remove the line totally ( data: emailData.serialize() ), then it hits my controller but doesn't pass any values... 根据我引用的链接,如果我完全删除该行( data: emailData.serialize() ),那么它将命中我的控制器,但不会传递任何值...

I have no idea how to resolve this. 我不知道该如何解决。

If it helps, my controller is simply 如果有帮助,我的控制器就是

    [HttpPost]
    public void RingMeBack(string RingMeBackName, string RingMeBackNumber)
    {
        //break point set just to see if hits this
        string s = "";
    }

As suggested in the comments, jQuery should do the conversion from the object to JSON for you, so you can just set the data parameter to data: emailData . 如评论中所建议,jQuery应该为您完成从对象到JSON的转换,因此您只需将data参数设置为data: emailData The reason you're then hitting the error function is because the controller isn't returning anything -- you're not seeing a successful ajax transaction. 之所以点击错误函数,是因为控制器没有返回任何内容-您没有看到成功的ajax事务。

The answer was due to a silly mistake 答案是由于一个愚蠢的错误

RingMeBackName: $("#ringMeBackName").val(),
RingMeBackNumber: $("#ringMeBackNumber").val()

Should have been 本来应该

'RingMeBackName': $("#ringMeBackName").val(),
'RingMeBackNumber': $("#ringMeBackNumber").val()

Note the single quote marks. 请注意单引号。

JSON.stringify(theObject) IS required JSON.stringify(theObject)是必需的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM