为什么jQuery.get、jQuery.getJSON、jQuery.post无法返回JSON
在WEB项目中,经常会使用到jQuery进行AJAX请求,但是自从使用了.net 3.5以后,以往写的请求语句就有些小问题了,就是返回的始终是xml,而并不是JSON,经过尝试,只需要在$.ajax中加入 contentType: "application/json; charset=utf-8"选项就可以了,这是因为在.net 3.5以后要对contentType进行检查,所以仅仅指定dataType的话.net就不会返回JSON了,那么我们的请求自然也就无法请求到JSON数据了。正确写法就是这样了:
出错代码
1 var url ="/Services/AccountService.asmx/UserExists";
2 var userName = $("#txtUserName").val();
3 $.ajax({
4 type: "POST",
5 url: url,
6 data: '{userName:"'+userName+'"}',
7 dataType: "json",
8 success: function (json) {
9 if (json.d ==true) {
10 $("#submit").removeAttr("disabled");
11 return;
12 }
13
14 $("#submit").attr("disabled", "disabled");
15 }
16 });
更正后代码
1 var url ="/Services/AccountService.asmx/UserExists";
2 var userName = $("#txtUserName").val();
3 $.ajax({
4 type: "POST",
5 url: url,
6 data: '{userName:"'+userName+'"}',
7 dataType: "json",
8 contentType: "application/json; charset=utf-8",
9 success: function (json) {
10 if (json.d ==true) {
11 $("#submit").removeAttr("disabled");
12 return;
13 }
14
15 $("#submit").attr("disabled", "disabled");
16 }
17 });
但是在使用$.get、$.getJSON、$.post时确怎么也得不到JSON数据了,写法如下:
$.get代码
1 var url ="/Services/AccountService.asmx/UserExists";
2 var userName = $("#txtUserName").val();
3 $.get(
4 url
5 , { userName: userName }
6 , function (json) {
7 if (json.d ==true) {
8 $("#submit").removeAttr("disabled");
9 return;
10 }
11
12 $("#submit").attr("disabled", "disabled");
13 },"json");
$.getJSON代码
1 var url ="/Services/AccountService.asmx/UserExists";
2 var userName = $("#txtUserName").val();
3 $.getJSON(
4 url
5 , { userName: userName }
6 , function (json) {
7 if (json.d ==true) {
8 $("#submit").removeAttr("disabled");
9 return;
10 }
11
12 $("#submit").attr("disabled", "disabled");
13 });
$.post代码
1 var url ="/Services/AccountService.asmx/UserExists";
2 var userName = $("#txtUserName").val();
3 $.post(
4 url
5 , { userName: userName }
6 , function (json) {
7 if (json.d ==true) {
8 $("#submit").removeAttr("disabled");
9 return;
10 }
11
12 $("#submit").attr("disabled", "disabled");
13 },'json');
用HttpWatch查看请求返回的数据如下:
显示代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <boolean xmlns="http://tempuri.org/">false</boolean>
看一下jQuery.extend中有关代码:
jQuery.extend
1 jQuery.extend({
2
3 get: function( url, data, callback, type ) {
4 // shift arguments if data argument was omited
5 if ( jQuery.isFunction( data ) ) {
6 type = type || callback;
7 callback = data;
8 data =null;
9 }
10
11 return jQuery.ajax({
12 type: "GET",
13 url: url,
14 data: data,
15 success: callback,
16 dataType: type
17 });
18 },
19
20 getScript: function( url, callback ) {
21 return jQuery.get(url, null, callback, "script");
22 },
23
24 getJSON: function( url, data, callback ) {
25 return jQuery.get(url, data, callback, "json");
26 },
27
28 post: function( url, data, callback, type ) {
29 // shift arguments if data argument was omited
30 if ( jQuery.isFunction( data ) ) {
31 type = type || callback;
32 callback = data;
33 data = {};
34 }
35
36 return jQuery.ajax({
37 type: "POST",
38 url: url,
39 data: data,
40 success: callback,
41 dataType: type
42 });
43 }
44 });
原因就是.net 3.5以后要对contentType进行检查,如果不为json的话,就不会返回json,而get、getJSON、post扩展都再次调用了ajax,但是只传了dataType参数,.net 3.5在检查contentType时发现其不是json,便返回了xml.