"Parserror" error when JQuery calls WebService through HttpGet to return Json

Source: Internet
Author: User

This article will tell you how to troubleshoot the "parserror" error when JQuery calls WebService to return Json through HttpGet. If you need it, refer to it.

First, let's look at the code that the client uses JQuery to call WebService:

 

The Code is as follows: Copy code

 

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 );
}
});


 

Server, WebService code:

 

The Code is as follows: Copy code

[WebMethod]
[ScriptMethod (ResponseFormat = ResponseFormat. Json)]
Public string HelloWorld (){
Return "Hello World ";
}
 

Use Fiddler to trace and find that the data returned by the server is in XML format after the client calls the server method. Why? Clearly, I have indicated on the method property that the returned json is returned, but the system still returns XML as usual?

So far, everyone's eyes are bright. Netizens at Home and Abroad unanimously pointed out that the. NET 3.5 platform needs to check the contentType parameter, so the code comment in the above Code is removed and re-run. An error occurs again. Check with Fiddler and find that the Server Returns Error 500. Specific error:

 

The Code is as follows: Copy code
{"Message": "try to use the GET request to call the" HelloWorld "method, but this is not allowed. "," StackTrace ":" In System. web. script. services. restHandler. getRawParams (WebServiceMethodData methodData, HttpContext context) rn in System. web. script. services. restHandler. executeWebServiceCall (HttpContext context, WebServiceMethodData methodData) "," ExceptionType ":" System. invalidOperationException "}

 

However, I have configured the WebService in the web. config file. Why is the server still not allowed to use the GET method? However, add the UseHttpGet attribute and set it to true. Then, check the Fiddler. OK. The server returns json data.

 

Let's take a look at Microsoft's code comments. The following section explains the above error prompt:

 

// True if the method is invoked by using the http get command; false if
// Method is invoked by using the http post command. The default is false.
 

So why can't GET be used in Web. config? This can only be interpreted as: Web. the configuration in the config file only allows WebService to receive Get requests. When using each method, you must configure the call method for this method. (if there is an error, please note that. Thank you !!).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.