After trying, you only need. 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:
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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
Copy codeThe 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:
Copy codeThe Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?>
<Boolean xmlns = "http://tempuri.org/"> false </boolean>
Let's take a look at the code in jQuery. extend:
JQuery. extend
Copy codeThe 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.