Send JSON with Ajax to ASP.NET Web Method

北城余情 提交于 2019-12-11 18:59:17

问题


This is first time I am attempting to send data to server using Ajax. I followed lot of answers from here but I won't just stop getting the error : "Message":"Invalid web service call, missing value for parameter: \u0027Products\u0027."

I followed this and this but still the same. Can someone please tell me where I am going wrong.

        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
    }

回答1:


Try this -

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



回答2:


Try this -

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

or

data : {Products: products}


来源:https://stackoverflow.com/questions/16751693/send-json-with-ajax-to-asp-net-web-method

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!