Jquery.get, Jquery.getjson, Jquery.post cannot return a solution to the JSON problem _jquery

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.