winthers
winthers

Reputation: 223

phonegap jquery $.get json

I'm having problems with jQuery in phonegap, the problem is when I load a .json file using $.get("file.json"), normally it will return all the data serialized as an Object, but in my app I just get a flat string.

So whats going on? Is phonegap missing a mime type for json or ?

$.get("file.json").done(function(data){

   typeof data // string

   // I can fix it like this, but I'll rather have the default behavior
   // of jquery.
   data = (typeof data == "string") ? JSON.parse(data) : data;    

});

Upvotes: 0

Views: 566

Answers (1)

Arun P Johny
Arun P Johny

Reputation: 388446

The problem could be the server may not be setting the proper MIME type (application/json) so tell jQuery explicitly that you are expecting json content from the server.

$.get("file.json", 'json').done(function(data){

   typeof data // string

   // I can fix it like this, but ill rather have the default behavior
   // of jQuery.
   console.log(data)  

});

Upvotes: 2

Related Questions