c# - 如何使用 $ajax 将 JSON 参数传递给 asmx web 服务

标签 c# jquery asp.net ajax asmx

我的参数如下:

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

伙计们请帮助我,我不明白我在做什么错。

最佳答案

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

// 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 对象。

关于c# - 如何使用 $ajax 将 JSON 参数传递给 asmx web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34305891/

相关文章:

c# - 可访问性不一致 : field type 'world' is less accessible than field 'frmSplashScreen

c# - 静态类中的对象实例

c# - Rhino Mocks,设置 stub 属性后如何执行操作

javascript - 添加时间的行值

javascript - LI 内的 DIV 元素

asp.net - asp.net 中的机器配置是什么

c# - 获取对象处置/销毁的通知

jquery - 如何在不提供任何现有容器的情况下将 jQuery.loadTemplate 放入新元素中?

c# - 翻译在 C# 中使用 Active Directory 的 VB

c# - 使用电子邮件地址的 Asp.Net 身份验证