【问题标题】:No parameterless constructor defined for type of 'System.String' during JSON deserialization calling c# webmethod [duplicate]在 JSON 反序列化调用 c# webmethod 期间没有为“System.String”类型定义无参数构造函数 [重复]
【发布时间】:2018-03-19 19:39:52
【问题描述】:

当我尝试从 JavaScript 调用我的 C# webmethod 时,我收到此错误“在 JSON 反序列化期间没有为 'System.String' 类型定义无参数构造函数”。

C#

using System.Web.Services;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

[WebMethod]
public static string insertValues(string param)
{
    JObject data = JObject.Parse(param);
    //data manipulation starts here

    return "finished";
}

JavaScript:

$.ajax({ type: 'GET', url: 'https://url_returning_json_object',
   success: function (data) {
         PageMethods.insertValues(data, function (ex) {
                console.log(ex);
          });
   }
});

有什么想法吗?

【问题讨论】:

  • 您将param 作为字符串而不是作为对象/类传递是否有特殊原因?
  • @mjwills 因为我将数据从经典的 ASP Web 应用程序传递到 C# 服务器端,我认为实现此目的的唯一方法是使用字符串。我只是想为面临同样问题的任何人提供解决方案。

标签: c# json


【解决方案1】:

我找到了解决方案:我需要在调用 webmethod 之前“字符串化”我的 json 对象。

$.ajax({ type: 'GET', url: 'https://url_returning_json_object',
   success: function (data) {
         //NEW LINE ADDED
         retData = JSON.stringify(data);

         PageMethods.insertValues(retData, function (ex) {
                console.log(ex);
          });
   }
});

【讨论】:

    猜你喜欢
    • 2012-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-31
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    相关资源
    最近更新 更多