Exclude the "parserror" error when JQuery uses HttpGet to call WebService to return Json

Source: Internet
Author: User
JQuery is often used. I didn't pay attention to it in the past. Recently, when I used JQuery to call WebService through HttpGet, I found that the server did not return json data as expected, but returned an error message: parserror. Now the problem is solved smoothly. The following is a solution... SyntaxHighlighter.

 

JQuery is often used. I didn't pay attention to it in the past. Recently, when I used JQuery to call WebService through HttpGet, I found that the server did not return json data as expected, but returned an error message: parserror.

Now the problem has been solved successfully. The following is the solution process (published on the blog homepage to check whether my explanation of the last problem is reasonable ).

 

First, let's look at the code that the client uses JQuery to call 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 );

}

});

 

Server, WebService 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:

 

{"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) \ r \ n 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 !!).

 

Source: www.cnblogs.com/jizhong

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.