我在向 .NET Web 服务提交复杂的 JSON 对象时遇到问题。
Ajax JavaScript 代码:
$.ajax({
type: "POST",
url: WEBSERVICE_ADMINISTRATIO_URL + "RecieveData",
data: JSON.stringify({data: _data}), // _data is the Javascipt object {}
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
alert(data);
},
failure: function (errMsg) {
alert(errMsg);
},
error: function (jqXHR, textStatus, errorThrow) {
alert(textStatus);
}
});
.NET Web 服务
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string RecieveData(string data)
{
return "";
}
当我尝试从客户端将数据提交到 Web 服务时,收到内部服务器错误 500。
我有其他网络服务功能,只返回数据,并且它们工作正常。我在 web.config 中授予了 GET 和 POST 权限。
这是详细的错误:
"{"Message":"No parameterless constructor defined for type of
\u0027System.String\u0027.","StackTrace":" at
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.Serialization.ObjectConverter.ConvertObjectToType(Object o, Type type, JavaScriptSerializer serializer)\r\n at System.Web.Script.Services.WebServiceMethodData.StrongTypeParameters(IDictionary`2 rawParams)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary`2 parameters)\r\n at System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext context, WebServiceMethodData methodData, IDictionary`2 rawParams)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.MissingMethodException"}"
谢谢。
最佳答案
感谢大家的支持。我通过将参数数据类型从字符串更改为对象来解决这个问题。
public string RecieveData(Object data)
{
return "";
}
关于c# - 将 JSON 对象发送到 .NET Web 服务时发生内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25228339/