Alex
Alex

Reputation: 68472

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

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.

Upvotes: 0

Views: 3908

Answers (3)

FatherStorm
FatherStorm

Reputation: 7183

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

Upvotes: 1

Teddy Yueh
Teddy Yueh

Reputation: 674

The .getJSON method also works across browsers =).

Upvotes: 1

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

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

Upvotes: 2

Related Questions