0

我有一个这样的 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 选项卡时什么都没有,但帖子有

因此,我在 web 服务中的断点永远不会被命中。

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

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

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

或者:

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

有什么提示吗?

4

3 回答 3

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');
        }
    });

问候。

于 2013-01-09T14:13:39.480 回答
1

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

    var dataToSend = {
           phoneReceiver: $("#receiver_phone").val(),
           emailNotify :$("#notify_email").val()
           value: $("#slider").slider("value")
    };
于 2013-01-09T14:15:47.333 回答
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');
                }
            });
    });
于 2013-01-09T14:16:10.913 回答