After trying, just add contentType to the $.ajax: "Application/json; Charset=utf-8 "option is OK, because the contenttype is checked after. NET 3.5, so just specify datatype. NET will not return JSON, then our request will naturally not be able to request the JSON data. The correct way to do this is:
Copy Code code 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 Code code 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");
}
});
But in the use of $.get, $.getjson, $.post do not get the JSON data, written as follows:
$.get Code
Copy Code code 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 Code code 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 Code code 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 ');
The data returned with the HttpWatch view request is as follows:
Copy Code code as follows:
<?xml version= "1.0" encoding= "Utf-8"?>
<boolean xmlns= "http://tempuri.org/" >false</boolean>
Take a look at the code in Jquery.extend:
Jquery.extend
Copy Code code 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 that after. NET 3.5, the ContentType is checked, and if not JSON, the JSON is not returned, and the GET, Getjson, Post extension calls Ajax again, but only the datatype parameters are passed,. Net 3.5 returns XML when it is not JSON when checking contenttype.