2

我正在使用 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'});
};

这是网络服务代码:

[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] 错误 - 服务器响应为:{"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 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串输入,Int32 depthLimit,JavaScriptSerializer 序列化程序)\r\n 在 System.Web.Script.Serialization .JavaScriptSerializer.Deserialize(JavaScriptSerializer 序列化程序,字符串输入,类型类型,Int32 depthLimit)\r\n 在 System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](字符串输入)\r\n 在 System.Web.Script .Services.RestHandler.GetRawParamsFromPostRequest(HttpContext 上下文,JavaScriptSerializer 序列化程序)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)" "ExceptionType":"System.ArgumentException"}

使用 ASP.Net webservice 测试页面,这里是我的标题和请求字符串: Cache-Control: private, max-age=0 Date: Wed, 03 Oct 2012 19:47:16 GMT Content-Length: 149 Content-Type:文本/xml;charset=utf-8 服务器:ASP.NET 开发服务器/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 jsonerror: true 服务器:ASP.NET 开发服务器/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

关于我为什么会收到此错误的任何想法?我试过用谷歌搜索它,但没有一个结果真的对我有帮助。

编辑:

如果我使用:数据:“{'jsonrpc':'2.0','method':'test','params':[],'id':1}”,

我收到此错误:[AJAX] 错误 - 服务器响应为:{“消息”:“无效的 Web 服务调用,参数缺失值:\u0027json\u0027。”,“StackTrace”:“在 System.Web.Script.Services .WebServiceMethodData.CallMethod(对象目标,IDictionary 2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary2 参数)\r\n 在 System.Web.Script.Services.RestHandler.InvokeMethod(HttpContext 上下文,WebServiceMethodData methodData,IDictionary`2 rawParams)\r\n 在 System.Web.Script .Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"}

如果我使用: 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 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal(Int32 depth)\r\n 在 System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(String input, Int32 depthLimit, JavaScriptSerializer 序列化程序)\r\n 在 System. Web.Script.Serialization.JavaScriptSerializer.Deserialize(JavaScriptSerializer 序列化程序,字符串输入,类型类型,Int32 depthLimit)\r\n at System.Web.Script.Serialization.JavaScriptSerializer.Deserialize[T](字符串输入)\r\n at System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext 上下文,JavaScriptSerializer 序列化程序)\r\n 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)" "ExceptionType":"System.ArgumentException"}

4

2 回答 2

3

将数据:“json=”+encodeURIComponent(request),更改为
数据:“{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-03T20:51:33.800 回答
2

尝试从 data 属性中删除 'json=',并确保 encodeURIComponent(request) 是一个 json 对象,我猜它不是。

于 2012-10-03T20:10:06.150 回答