Search code examples
c#jqueryasp.netajaxasmx

How to pass JSON parameter with $ajax to asmx web service


My paramerter as below:

var pMaster = '{"tid" : "474", "fid":"2"}';
var pDetail = '[{"recid":5618,"tid":"474","itemid":"1435","nar1":""},{"recid":5619,"tid":"474","itemid":"1203","nar1":""},{"recid":5620,"tid":"474","itemid":"1205","nar1":""}]';
var e = '{PurcMast: ' + pMaster  + ', PurDetail: ' + pDetail + '}';

I am calling ajax as below

$.ajax({
        type: "POST",
        url: "WebService.asmx/saveValue",
        data: e,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            alert(result.d);
        },
        error: function (jqXHR) { alert(jqXHR.responseText); }
    });

And WebService.asmx code as below:

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
[WebMethod]    
public void saveValue(string PurcMast, string PurDetail)
{
    System.Data.DataTable purMaster = Common.CommonFunction.convertJSON2Table(Purchase);
    System.Data.DataTable purDetail = Common.CommonFunction.convertJSON2Table(PurchaseDetail);
}

I am getting error as below:

Uncaught Error.{"Message":"No parameterless constructor defined for type of \u0027System.String\u0027.","StackTrace":" at System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\u0026 convertedObject)\r\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary2 rawParams)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.MissingMethodException"}

Guys please help me, i don't understand what wrong i am doing.


Solution

  • I do this on my site. This is what I had to do...

    // remove the outer quotes so they are json objects then json encode.
    var pMaster = JSON.stringify({"tid" : "474", "fid":"2"});
    var pDetail = JSON.stringify(
        {"[{"recid":5618,"tid":"474","itemid":"1435","nar1":""}, 
        {"recid":5619,"tid":"474","itemid":"1203","nar1":""},
        {"recid":5620,"tid":"474","itemid":"1205","nar1":""}]);
    // then create e by stringify a second time
    
    var e = JSON.stringify({PurcMast: pMaster , PurDetail: pDetail });
    

    This worked for me. You were just creating strings instead serialized json objects.