JQuery. get, jQuery. getJSON, and jQuery. post cannot return a solution to the JSON problem _ jquery-js tutorial

Source: Internet
Author: User
In WEB projects, jQuery is often used for AJAX requests. after net3.5, the previously written request statements have some minor issues, that is, the returned results are always xml, rather than JSON. add the contentType: "application/json; charset = UTF-8" option in ajax.. net 3.5 and later, you must check the contentType. net will not return JSON, so our request will naturally be unable to request JSON data. The correct method is as follows:

The Code is as follows:


Var url = "/Services/AccountService. asmx/UserExists ";
Var userName = $ ("# txtUserName"). val ();
$. Ajax ({
Type: "POST ",
Url: url,
Data: '{userName: "' + userName + '"}',
DataType: "json ",
Success: function (json ){
If (json. d = true ){
$ ("# Submit"). removeAttr ("disabled ");
Return;
}
$ ("# Submit"). attr ("disabled", "disabled ");
}
});


Corrected code

The Code is as follows:


Var url = "/Services/AccountService. asmx/UserExists ";
Var userName = $ ("# txtUserName"). val ();
$. Ajax ({
Type: "POST ",
Url: url,
Data: '{userName: "' + userName + '"}',
DataType: "json ",
ContentType: "application/json; charset = UTF-8 ",
Success: function (json ){
If (json. d = true ){
$ ("# Submit"). removeAttr ("disabled ");
Return;
}
$ ("# Submit"). attr ("disabled", "disabled ");
}
});


However, when $. get, $. getJSON, and $. post are used, the JSON data cannot be obtained as follows:
$. Get code

The Code is as follows:


Var url = "/Services/AccountService. asmx/UserExists ";
Var userName = $ ("# txtUserName"). val ();
$. Get (
Url
, {UserName: userName}
, Function (json ){
If (json. d = true ){
$ ("# Submit"). removeAttr ("disabled ");
Return;
}
$ ("# Submit"). attr ("disabled", "disabled ");
}, "Json ");


$. GetJSON code

The Code is as follows:


Var url = "/Services/AccountService. asmx/UserExists ";
Var userName = $ ("# txtUserName"). val ();
$. GetJSON (
Url
, {UserName: userName}
, Function (json ){
If (json. d = true ){
$ ("# Submit"). removeAttr ("disabled ");
Return;
}
$ ("# Submit"). attr ("disabled", "disabled ");
});


$. Post code

The Code is as follows:


Var url = "/Services/AccountService. asmx/UserExists ";
Var userName = $ ("# txtUserName"). val ();
$. Post (
Url
, {UserName: userName}
, Function (json ){
If (json. d = true ){
$ ("# Submit"). removeAttr ("disabled ");
Return;
}
$ ("# Submit"). attr ("disabled", "disabled ");
}, 'Json ');


Use HttpWatch to view the data returned by the request as follows:

The Code is as follows:



False


Let's take a look at the code in jQuery. extend:
JQuery. extend

The Code is as follows:


JQuery. extend ({
Get: function (url, data, callback, type ){
// Shift arguments if data argument was omited
If (jQuery. isFunction (data )){
Type = type | callback;
Callback = data;
Data = null;
}
Return jQuery. ajax ({
Type: "GET ",
Url: url,
Data: data,
Success: callback,
DataType: type
});
},
GetScript: function (url, callback ){
Return jQuery. get (url, null, callback, "script ");
},
GetJSON: function (url, data, callback ){
Return jQuery. get (url, data, callback, "json ");
},
Post: function (url, data, callback, type ){
// Shift arguments if data argument was omited
If (jQuery. isFunction (data )){
Type = type | callback;
Callback = data;
Data = {};
}
Return jQuery. ajax ({
Type: "POST ",
Url: url,
Data: data,
Success: callback,
DataType: type
});
}
});


The reason is. net 3.5 and later, you need to check the contentType. If it is not json, no json is returned, and the get, getJSON, and post extensions call ajax again, however, only the dataType parameter is passed ,. net 3.5 checks contentType and finds that it is not json and then returns xml.
Related Article

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.