【问题标题】:How to pass JSON parameter with $ajax to asmx web service如何将带有 $ajax 的 JSON 参数传递给 asmx Web 服务
【发布时间】:2015-12-16 07:08:48
【问题描述】:

我的参数如下:

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(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(IDictionary 2 rawParams)\r\n 在 System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(对象目标,IDictionary2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary2 rawParams)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext 上下文, WebServiceMethodData methodData)","ExceptionType":"System.MissingMethodException"}

各位大侠帮帮我,我不明白我做错了什么。

【问题讨论】:

  • 道歉哥们,我只是将值更改为字符串。我用 JSON.stringify() 试过了,但还是一样的错误。
  • 源 JSON 中的 PurcMastPurDetail 道具周围没有“”。这是无效的 JSON。
  • 你的 URL 是 url: "WebService.asmx/saveValue" 而方法名是 savePurchase
  • @Alisagar,也更新了,但没有调用 Web 服务。

标签: c# jquery asp.net ajax asmx


【解决方案1】:

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

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

【讨论】:

  • 在执行此操作时,我遇到了日期问题。 /Date(1234564)/ 格式的日期被双重转义,并且不会在后端反序列化。我的第一个修复是将格式更改为格式化为字符串的 yyyy-mm-dd,这很有效。但后来我开始使用 Newtonsoft Json 序列化程序,它已经把它变成了那种格式。
【解决方案2】:

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

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

【讨论】:

  • 无效的 JSON 原语:PurcMast
【解决方案3】:

您的 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); }
    });

我认为这应该可行。

【讨论】:

  • 我变了。这是我的错误。道歉。无论如何,没有调用 Web 服务。某处显示 javascript 脚本错误。
猜你喜欢
  • 1970-01-01
  • 2019-03-26
  • 2018-12-06
  • 2018-12-07
  • 2014-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多