我正在使用JQuery和Ajax与ASP.Net web服务进行交互。下面是Jquery代码:
$.jrpc = function(url, id, method, params, success, error) {
var request = $.json_stringify({
'jsonrpc': '2.0', 'method': method,
'params': params, 'id': id});
return $.ajax({
url: url,
data: "json=" + encodeURIComponent(request),
success: success,
error: error,
contentType: 'application/json; charset=utf-8',
dataType: 'json',
async: true,
cache: false,
//timeout: 1,
type: 'POST'});
};以下是web服务代码:
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ScriptService]
public class Access : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string EntryMethod(string json)
{
Requests d = JsonConvert.DeserializeObject<Requests>(json);
Response resp = new Response();
resp.jsonrpc = d.jsonrpc;
resp.result = "Got the call!";
resp.id = d.id;
resp.error = null;
JavaScriptSerializer response = new JavaScriptSerializer();
string r = response.Serialize(resp);
return r;
}
}我得到了这个错误:
AJAX错误-服务器响应为:{“System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n”:“无效的JSON原语: json.","StackTrace":”at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer depth)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer input,Int32 depthLimit,JavaScriptSerializer serializer)\r\n at json serializer,String input,Type type,序列化程序)\r\n System.Web.Script.Serialization.JavaScriptSerializer.DeserializeT\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext depthLimit,JavaScriptSerializer methodData )\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData context,HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,HttpContext methodData)","ExceptionType":"System.ArgumentException"}
使用AspNet webservice测试页时,以下是我的标题和请求字符串: ASP.Net -Control: private,max-age=0 Date: Wed,03 Oct 2012 19:47:16 GMT Content-Length: 149 Content-Type: text/xml;charset=utf-8服务器:AspNet Development Server/10.0.0.0 X-AspNet-Version: 4.0.30319连接:关闭
json=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22test%22%2C%22params%22%3A%5B%5D%2C%22id%22%3A1%7D
下面是不起作用的Ajax调用的头/请求字符串: Cache-Control: private Date: Wed,03 Oct 2012 19:47:09 GMT Content-Length: 1062 Content-Type: application/json;charset=utf-8 json Server: true Server: ASP.NET Development Server/10.0.0.0 X-AspNet-Version: 4.0.30319 Connection: Close
json=%7B%22jsonrpc%22%3A%222.0%22%2C%22method%22%3A%22test%22%2C%22params%22%3A%5B%5D%2C%22id%22%3A1%7D
你知道为什么我会得到这个错误吗?我试着用谷歌搜索它,但没有一个结果对我有真正的帮助。
编辑:
如果我使用: data:"{'jsonrpc':'2.0','method':'test','params':[],'id':1}",
我收到此错误: AJAX错误-服务器响应为:{“Message”:“无效的System.Web.Script.Services.WebServiceMethodData.CallMethod(Object服务调用,缺少参数值:\u0027json\u0027.","StackTrace":”AJAX目标,IDictionary2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2参数)\r\n AJAX context,WebServiceMethodData methodData,IDictionary`2 rawParams)\r\n AJAX context,WebServiceMethodData methodData)",AJAX
如果我使用: data:"json={'jsonrpc':'2.0','method':'test','params':[],'id':1}",
我收到此错误: AJAX错误-服务器响应为:{"Message":"Invalid JSON primitive: json.","StackTrace":“at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n at System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input,Int32 depthLimit,JavaScriptSerializer serializer)\r\n at json serializer,String input,Type type,序列化程序)\r\n System.Web.Script.Serialization.JavaScriptSerializer.DeserializeT\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext depthLimit,JavaScriptSerializer methodData )\r\n at System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData context,HttpContext context)\r\n at System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,HttpContext methodData)","ExceptionType":"System.ArgumentException"}
发布于 2012-10-04 04:51:34
修改数据:"json=“+ encodeURIComponent(request),改为
data: "{json: '" + encodeURIComponent(request)+"'}",请参阅示例代码
$.ajax("WebService.asmx/EntryMethod", {
contentType: "application/json; charset=utf-8",dataType: "json",
type: "POST", data: "{json:'value'}", success: function (r) { alert(r); },
error: function (e) { alert(e); }
});发布于 2012-10-04 04:10:06
尝试从data属性中删除'json=‘,并确保encodeURIComponent(request)是一个json对象,我猜它不是。
https://stackoverflow.com/questions/12715958
复制相似问题