Sending JSON object successfully to ASP.NET WebMethod, using jQuery

后端 未结 6 1416
执念已碎
执念已碎 2020-12-05 00:55

I\'ve been working on this for 3 hours and have given up. I am simply trying to send data to an ASP.NET WebMethod, using jQuery. The data is basically a bunch of key/value p

6条回答
  •  醉梦人生
    2020-12-05 01:29

    In your example, it should work if your data parameter is:

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

    Keep in mind that you need to send a JSON string to ASP.NET AJAX. If you specify an actual JSON object as jQuery's data parameter, it will serialize it as &k=v?k=v pairs instead.

    It looks like you've read it already, but take another look at my example of using a JavaScript DTO with jQuery, JSON.stringify, and ASP.NET AJAX. It covers everything you need to make this work.

    Note: You should never use JavaScriptSerializer to manually deserialize JSON in a "ScriptService" (as suggested by someone else). It automatically does this for you, based on the specified types of the parameters to your method. If you find yourself doing that, you are doing it wrong.

提交回复
热议问题