Passing parameter to WebMethod with jQuery Ajax

后端 未结 2 1791
时光取名叫无心
时光取名叫无心 2020-12-06 05:31

I have a simple web method and ajax call and continue to recieve an error that saying that it can\'t convert a string to an IDictionary object???

Here is the ajax ca

相关标签:
2条回答
  • 2020-12-06 06:14

    In addition to the above, it is worth checking to make sure that you aren't 'stringifying' the JSON array more than once.

    I accidentally called JSON.stringify() on an array that had already been serialized, which threw a similar issue to the one the OP received.

    i.e.

    var arr = JSON.stringify({ id: elementID, name: Name });
    ....
    $.ajax({
    ...
    data: JSON.stringify(arr),
    ...
    });
    

    In this instance, changing the arr variable initialization to

    var arr = { id: elementID, name: Name };
    

    resolved my issue. :)

    0 讨论(0)
  • 2020-12-06 06:16

    Quick item:

    your variable params var params = '{ID:' + rowid + '}'; is a string.

    So the line: data: JSON.stringify(params), is redundant (or it should be). Just set data: params,

    Next up, on your web method, you converting your result to a JSON string and returning that as a string. If you web method class has ScriptMethod attribute, you don't need to do that. Just return the data as the native type, and Asp.Net will do the conversion to JSON for you.

    You might read the following articles: http://elegantcode.com/2009/02/21/javascript-arrays-via-jquery-ajax-to-an-aspnet-webmethod/

    http://encosia.com/2008/03/27/using-jquery-to-consume-aspnet-json-web-services/

    0 讨论(0)
提交回复
热议问题