如何将带有 $ajax 的 JSON 参数传递给 asmx Web 服务

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

我的参数如下:

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 + '}';

我正在调用 ajax 如下

$.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); }
    });

和WebService.asmx代码如下:

[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);
}

我收到如下错误:

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"}

大家帮帮我,我不明白我做错了什么。

我猜你以错误的方式传递数据。试试这个:

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":""}]';

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

您的 URL 是 url: "WebService.asmx/saveValue" 而方法名称是 savePurchase

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

我认为这应该可行。

我在我的网站上这样做。这是我必须做的...

// 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 });

这对我有用。您只是在创建字符串而不是序列化 json 个对象。