Python语言技术文档

微信小程序技术文档

php语言技术文档

jsp语言技术文档

asp语言技术文档

C#/.NET语言技术文档

html5/css技术文档

javascript

点击排行

排除JQuery通过HttpGet调用WebService返回Json时“parserror”错误

来源:中文源码网    浏览:291 次    日期:2024-05-08 02:44:11
【下载文档:  排除JQuery通过HttpGet调用WebService返回Json时“parserror”错误.txt 】


排除JQuery通过HttpGet调用WebService返回Json时“parserror”错误
JQuery大家都经常用,以前用的时候没有注意什么。最近本人在使用JQuery通过HttpGet方式调用WebService时,却发现服务端并非如人所愿返回json数据,而是返回错误提示:parserror。 如今问题被顺利解决,下面是解决过程 首先看客户端使用JQuery调用WebService的代码: 复制代码 代码如下: getHellobyAjax: function(callabckFun) { $.ajax({ type: "GET", url: "WebService.asmx/HelloWorld", //contentType: "application/json; charset=utf-8", //data:"{}", cache: false, dataType: "json", success: function(msg) { if (callabckFun) { callabckFun(msg); } else { alert("Not exists callback function."); } }, error: function(obj, message) { alert(message); } }); 服务端,WebService的代码为: 复制代码 代码如下: [WebMethod] [ScriptMethod(ResponseFormat = ResponseFormat.Json)] public string HelloWorld() { return "Hello World"; } 使用Fiddler跟踪,发现客户端调用服务器方法后,服务器返回的数据为XML格式。Why? 明明自己已经在方法属性上指明返回json,但是系统却还是我行我素照常返回XML呢? 到此,大家的眼睛都是雪亮的。海内外的网友一致指出.NET 3.5平台是需要检查contentType参数的,于是将上面代码中的代码注释去除,重新运行。这时又出现error错误。用Fiddler一查,发现是服务器返回了500错误。具体错误为: 复制代码 代码如下: {"Message":"试图使用 GET 请求调用方法“HelloWorld”,但不允许这样做。","StackTrace":" 在 System.Web.Script.Services.RestHandler.GetRawParams(WebServiceMethodData methodData, HttpContext context)\r\n 在 System.Web.Script.Services.RestHandler.ExecuteWebServiceCall(HttpContext context, WebServiceMethodData methodData)","ExceptionType":"System.InvalidOperationException"} 可是,按理说,我已经在web.config文件中对WebService做了相应的配置,为什么服务器还是不允许使用GET方式调用呢?无奈,将UseHttpGet属性加上,并设置其为true,再祭出Fiddler一查,OK,服务器返回了json格式的数据。 再一看微软的代码注释,有如下一段,正好解释了上面的错误提示: 复制代码 代码如下: // true if the method is invoked by using the HTTP GET command; false if the // method is invoked by using the HTTP POST command. The default is false. 那么为什么Web.config已经允许使用GET,却不起作用呢?这只能解释为:Web.config文件中的配置只是配置允许WebService接收Get请求,具体到每一个方法时,还必须要配置该方法的调用方式才行(如有错误,请指出。谢谢!!)。 有人回复: 将ws的scriptmethod那句改为:[System.Web.Script.Services.ScriptService] js中启用content type 文章出处:www.cnblogs.com/jizhong

相关内容