[英]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);
}
我收到如下錯誤:
未捕獲的錯誤。{“消息”:“沒有為\\ u0027System.String \\ u0027的類型定義無參數構造函數。”,“StackTrace”:“在System.Web.Script.Serialization.ObjectConverter.ConvertDictionaryToObject(IDictionary
2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\& convertedObject)\\r\\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\& convertedObject)\\r\\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\& convertedObject)\\r\\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary
2 rawParams)中的2 dictionary, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\& convertedObject)\\r\\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeInternal(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\& convertedObject)\\r\\n at System.Web.Script.Serialization.ObjectConverter.ConvertObjectToTypeMain(Object o, Type type, JavaScriptSerializer serializer, Boolean throwOnError, Object\& convertedObject)\\r\\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary
\\ r \\ n在System.Web.Script.Script.WebServiceMethodData.CallMethodFromRawParams(對象目標,IDictionary2 parameters)\\r\\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary
2 parameters)\\r\\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary
2 rawParams)\\ r \\ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMethodData methodData)“,”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); }
});
我認為這應該有效。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.