得到500错误

时间:2012-10-03 20:01:27

标签: jquery asp.net ajax json web-services

我正在使用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”:“无效的JSON原语:json。”,“StackTrace”:“在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal上的System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\ r \ n” System.Web.Script.Serialization.JavaScript序列化程序,字符串处于System.Web.Script.Serialization.JavaScript序列化程序,字符串处理器中的System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串输入,Int32 depthLimit,JavaScriptSerializer序列化程序)\ r \ n的Int32深度)\ r \ n System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context)中的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串输入)\ r \ n的输入,类型类型,Int32 depthLimit)\ r \ n ,JavaScriptSerializer序列化程序)\ r \ n在System.Web.Script.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext context)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMetho) dData methodData)“,”ExceptionType“:”System.ArgumentException“}

使用ASP.Net webservice测试页面,这里是我的标题和请求字符串: Cache-Control:private,max-age = 0 日期:2012年10月3日星期三19:47:16 GMT 内容长度:149 Content-Type:text / xml;字符集= utf-8的 服务器:ASP.NET 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调用的头文件/请求字符串,它不起作用: 缓存控制:私有 日期:2012年10月3日星期三19:47:09 GMT 内容长度:1062 Content-Type:application / json;字符集= utf-8的 jsonerror:是的 服务器:ASP.NET 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

为什么我收到此错误的任何想法?我试过谷歌搜索它,但结果都没有真正帮助我。

编辑:

如果我使用:     数据:“{'jsonrpc':'2.0','方法':'测试','参数':[],'id':1}”,

我收到此错误: [AJAX]错误 - 服务器响应是: {“Message”:“无效的Web服务调用,缺少参数值:\ u0027json \ u0027。”,“StackTrace”:“at System.Web.Script.Services.WebServiceMethodData.CallMethod(Object target,IDictionary 2 parameters)\r\n at System.Web.Script.Services.WebServiceMethodData.CallMethodFromRawParams(Object target, IDictionary 2个参数)\ r \ n在System.Web.Script.Script.Services.RestHandler.InvokeMethod(HttpContext context,WebServiceMethodData methodData,IDictionary`2 rawParams)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context) ,WebServiceMethodData methodData)“,”ExceptionType“:”System.InvalidOperationException“}

如果我使用:     数据:“json = {'jsonrpc':'2.0','方法':'测试','参数':[],'id':1}”,

我收到此错误: [AJAX]错误 - 服务器响应是: {“Message”:“无效的JSON原语:json。”,“StackTrace”:“在System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializeInternal上的System.Web.Script.Serialization.JavaScriptObjectDeserializer.DeserializePrimitiveObject()\ r \ n” System.Web.Script.Serialization.JavaScript序列化程序,字符串处于System.Web.Script.Serialization.JavaScript序列化程序,字符串处理器中的System.Web.Script.Serialization.JavaScriptObjectDeserializer.BasicDeserialize(字符串输入,Int32 depthLimit,JavaScriptSerializer序列化程序)\ r \ n的Int32深度)\ r \ n System.Web.Script.Services.RestHandler.GetRawParamsFromPostRequest(HttpContext context)中的System.Web.Script.Serialization.JavaScriptSerializer.Deserialize [T](字符串输入)\ r \ n的输入,类型类型,Int32 depthLimit)\ r \ n ,JavaScriptSerializer序列化程序)\ r \ n在System.Web.Script.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData,HttpContext context)\ r \ n在System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context,WebServiceMetho) dData methodData)“,”ExceptionType“:”System.ArgumentException“}

2 个答案:

答案 0 :(得分:3)

变化      data:“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); }
    });

答案 1 :(得分:2)

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