如何将带有$ajax的JSON参数传递给asmx web服务
本文关键字:参数传递 JSON asmx 服务 web ajax | 更新日期: 2023-09-27 18:26:54
我的参数如下:
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);
}
我得到的错误如下:
未捕获错误。{"Message":"未为类型''u0027System.String''u0027."、"StackTrace":"在System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary
2 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(IDictionary
2 rawParams)''r''n在System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象目标,IDictionic2 parameters)'r'n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary
2 2 rawParams)定义无参数构造函数"''''r''n位于System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext上下文,WebServiceMethodData方法数据)","ExceptionType":"System.MissingMethodException"}
伙计们请帮帮我,我不明白我做错了什么。
我在我的网站上做这件事。这就是我必须做的…
// 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对象。
我猜您传递数据的方式不对。试试这个:
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); }
});
我认为这应该奏效。