jQuery ajax调用不是作为json调用触发的

时间:2013-01-09 13:35:00

标签: jquery asp.net-mvc json

我有这样的jQuery调用,这给了我很多问题:

  $('#submit').click(function () {
        var url = "/home/start";
        var notifyEmail = $("#notify_email").val();
        var receiverPhone = $("#receiver_phone").val();
        var sliderValue = $("#slider").slider("value");

        var dataToSend = '{phoneReceiver:' + receiverPhone + ', emailNotify:' + notifyEmail + ', value:' + sliderValue + '}';
        //var dataToSend = '{"phoneReceiver":"' + receiverPhone + '", "emailNotify":"' + notifyEmail + '", "value:"' + sliderValue + '"}';

        $.ajax({
            type: "POST",
            url: url,
            data: dataToSend,
            dataType: 'json',
            contentType: "application/json; charset=utf-8",
            success: function (data) {
                alert('Awesome destination: ' + data.DestinationAddress);

            },
            error: function (date) {
                alert('An occurred while purchasing. Please try again later');
            }
        });
    });

我试过摆弄数据格式化(如你所见,有两个版本)和/或没有dataType和contentType。没有运气了。

我有以下问题:

  • 我收到500内部服务器错误,看起来像格式化错误
  • 此调用不是作为FireBug中的json进行的。选择json选项卡时没有任何内容,但帖子有

因此,我的网络服务中的断点永远不会被击中。

数据中的所有参数都很好。

在FireBug中,我可以看到我的帖子是:

{phoneReceiver:fgsdfg, emailNotify:dgsg, value:19}

或者:

{"phoneReceiver":"gfjhfghj", "emailNotify":"fjhfgjhgj", "value:"16"}

任何提示?

3 个答案:

答案 0 :(得分:1)

试试这个......

$.ajax({
        type: "POST",
        url: url,
        data: { phoneReceiver: receiverPhone, emailNotify: notifyEmail,  value: sliderValue},
        dataType: 'json',
        contentType: "application/json; charset=utf-8",
        success: function (data) {
            alert('Awesome destination: ' + data.DestinationAddress);

        },
        error: function (date) {
            alert('An occurred while purchasing. Please try again later');
        }
    });

问候。

答案 1 :(得分:1)

如果您要发送JSON,请尝试将dataToSend对象创建为

    var dataToSend = {
           phoneReceiver: $("#receiver_phone").val(),
           emailNotify :$("#notify_email").val()
           value: $("#slider").slider("value")
    };

答案 2 :(得分:1)

我能够按照以下方式使用您的代码:

    //action
    [HttpPost]
    public void TestAction(string phoneReceiver, string emailNotify, int value)
    {
        //all variables set here
    }


    //in view i have a button id = submit
    $('#submit').click(function () {
            var dataToSend = '{phoneReceiver: "blah", emailNotify:"blah@blah.com", value: 1}';

            $.ajax({
                type: "POST",
                url: '/TestController/TestAction',
                data: dataToSend,
                dataType: 'json',
                contentType: "application/json; charset=utf-8",
                success: function (data) {
                    alert('Awesome destination: ' + data.DestinationAddress);

                },
                error: function (date) {
                    alert('An occurred while purchasing. Please try again later');
                }
            });
    });