Home > Web Front-end > JS Tutorial > Solutions to the problem that jQuery.get, jQuery.getJSON, and jQuery.post cannot return JSON_jquery

Solutions to the problem that jQuery.get, jQuery.getJSON, and jQuery.post cannot return JSON_jquery

WBOY
Release: 2016-05-16 18:04:21
Original
1009 people have browsed it

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:

Copy 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",
success: function (json) {
if (json.d == true) {
$("#submit").removeAttr("disabled");
return;
}
$("#submit").attr("disabled", "disabled");
}
});

Corrected code
Copy 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");
}
});

But in When using $.get, $.getJSON, $.post, I cannot get JSON data. The writing method is as follows:
$.get code
Copy 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
Copy 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
Copy 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:
Copy code The code is as follows:


false

Look at the relevant code in jQuery.extend:
jQuery.extend
Copy code 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
});
}
});

原因就是.net 3.5以后要对contentType进行检查,如果不为json的话,就不会返回json,而get、getJSON、post扩展都再次调用了ajax,但是只传了dataType参数,.net 3.5在检查contentType时发现其不是json,便返回了xml.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template