【问题标题】:Send JSON with Ajax to ASP.NET Web Method使用 Ajax 将 JSON 发送到 ASP.NET Web 方法
【发布时间】:2013-05-21 01:00:42
【问题描述】:

这是我第一次尝试使用 Ajax 向服务器发送数据。我从这里得到了很多答案,但我不会停止收到错误:"Message":"Invalid web service call, missing value for parameter: \u0027Products\u0027."

我关注了thisthis,但还是一样。谁能告诉我哪里出错了。

        var products =  [ { ProductId : 1, ProductName : "Mercedes", Category : "Cars", Price : 25000 }, { ProductId : 2, ProductName : "Otobi", Category : "Furniture", Price : 20000 } ];

    function GetProductId() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/GenerateQrCode",
            data: { "Products" : products.toString()  },
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            error: function (xhr, ajaxOptions, thrownError) {
                alert(xhr.status);
                alert(xhr.responseText);
                alert(thrownError);
            },
            success: function (msg) {
                alert('Success');
            }
        });
    }


[WebMethod]
    public static void GenerateQrCode(object Products)
    {
        //Cannot get to here
    }

【问题讨论】:

  • try : data: { Products : products }, jQuery 会为你解析对象-
  • 这不起作用。同样的错误。

标签: jquery ajax json webmethod


【解决方案1】:

试试这个 -

data : "{'Products':" + JSON.stringify(products) + "}"

【讨论】:

  • 谢谢。您的解决方案正在运行,但它仅适用于 Chrome,但不适用于 IE10。你知道为什么吗?
【解决方案2】:

试试这个 -

data : {Products: JSON.stringify(products)}

data : {Products: products}

【讨论】:

    猜你喜欢
    • 2016-11-07
    • 1970-01-01
    • 2010-12-04
    • 2011-02-08
    • 2018-11-21
    • 1970-01-01
    • 2019-06-28
    • 1970-01-01
    • 2012-06-12
    相关资源
    最近更新 更多