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.