After trying, you only need to add the contentType: "application/json; charset=utf-8" option to $.ajax. This is because the contentType needs to be checked after .net 3.5, so if you only specify the dataType.net JSON will not be returned, so our request will naturally not be able to request JSON data. The correct way to write it is like this:
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
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 When using $.get, $.getJSON, $.post, I cannot get JSON data. The writing method is as follows:
$.get code
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
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
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:
false
Look at the relevant code in jQuery.extend:
jQuery.extend
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
});
}
});
原因就是.net 3.5以后要对contentType进行检查,如果不为json的话,就不会返回json,而get、getJSON、post扩展都再次调用了ajax,但是只传了dataType参数,.net 3.5在检查contentType时发现其不是json,便返回了xml.