2013-09-27 3 views
0

Je vais avoir des problèmes avec jQuery dans PhoneGap, le problème est quand je charge un fichier en utilisant .json $.get("file.json"), normalement il retournera toutes les données sérialisé comme un objet, mais dans mon application je viens obtenir une chaîne plate.

Alors, que se passe-t-il? Phonegap manque-t-il un type mime pour json ou?

$.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;  

}); 

Répondre

2

Le problème pourrait être le serveur peut pas définir le type MIME correct (application/json) afin dire explicitement jQuery que vous attendez le contenu JSON du serveur.

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

}); 
+0

Il travaille :) mais je devais le faire comme ceci: .get $ ("file.json", la fonction (données) {}, "JSON"); – winthers