开发者

jQuery, AJAX - content type: application/json - does it work in all browsers?

开发者 https://www.devze.com 2023-02-05 00:00 出处:网络
This is how I\'ve been handling my ajax until now: @header(\"Content-Type: text/html; charset=\".get_option(\'blog_charset\'));

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 =).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号