无法通过Ajax访问我的控制器

时间:2014-05-17 17:19:50

标签: javascript ajax

我第一次尝试使用Ajax。

我收到错误的问题:uncaught referenceError: Data is not defined我可以在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
        }

如果我将data: JSON.stringify(emailData)更新为data: emailData.serialize(),则没有任何区别。

根据我引用的链接,如果我完全删除该行(data: emailData.serialize()),那么它会触及我的控制器,但不会传递任何值......

我不知道如何解决这个问题。

如果有帮助,我的控制器就是

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

2 个答案:

答案 0 :(得分:0)

正如评论中所建议的,jQuery应该为您进行从对象到JSON的转换,因此您只需将数据参数设置为data: emailData即可。你之所以点击错误功能是因为控制器没有返回任何内容 - 你没有看到成功的ajax事务。

答案 1 :(得分:0)

答案是由于一个愚蠢的错误

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

应该是

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

请注意单引号。

JSON.stringify(theObject)是必需的