Search code examples
jqueryajaxjsonwebmethod

Send JSON with Ajax to ASP.NET Web Method


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
    }

Solution

  • Try this -

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