jQuery, AJAX - content type: application/json - does it work in all browsers?
This is how I've been handling my ajax until now:
@header("Content-Type: text/html; charset=".get_option('blog_charset'));
and the js:
$.ajax(....
...
success: function(response){
var obj = eval('('+response+')');
if(obj.somedata == ....)
...
And now I want to use application/json
as content type so the javascript changes to:
$.ajax(....
...
success: function(response){
if(response.somedata == .开发者_StackOverflow社区...)
...
Looks better :) But I'm curious to know if this will work in all browsers? So far it tested OK in FF, Opera and IE 8.
Yes, it works as long as your server sends proper content-type (application/json) and properly formated JSON data.
Also just for safety you could specify the response data type:
$.ajax({
dataType: 'json',
success: function(response) {
if(response.somedata == ....
}
});
yes. JSON is not so much browser-dependent/specific as it is Javascript-specific, so assuming you have a browser (any browser) that has a full implementation of javascript, then it will support JSON. see here
The .getJSON method also works across browsers =).
精彩评论