使用Ajax将JSON发送到ASP.NET Web方法 [英] Send JSON with Ajax to ASP.NET Web Method

查看:121
本文介绍了使用Ajax将JSON发送到ASP.NET Web方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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
    }

推荐答案

尝试一下-

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

这篇关于使用Ajax将JSON发送到ASP.NET Web方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆