.getJSON $ () fonctionne dans IE9, mais pas dans Chrome et FireFox, ce qui est erroné dans mon code?

StackOverflow https://stackoverflow.com/questions/8371570

Question

Je développé une application Web à l'aide IE9 et un éditeur de texte. Il lit un fichier JSON, remplit alors certains éléments DIV selon ce fichier et la logique du code JavaScript et jQuery. Sous IE9, il fonctionne parfaitement.

Dans Chrome, il ne parvient pas à exécuter l'instruction .getJSON $ (), donc pas de données disponibles. Sous FireFox, la déclaration .getJSON () $ fonctionne apparemment (messages d'alerte en témoignent), mais il ne lit rien.

Le fichier JSON passe JSONLint.

Ni Chrome ni FireFox indiquent des erreurs.

J'ai fait un exemple de fichier à l'aide des données JSON à partir du site JSON, validé par une JSONLint, puis a couru mon code en utilisant ce fichier. Pas de différence -. Chrome ignore encore la déclaration .getJSON $ ()

Une section pertinente de mon code:

    function buildTree(centralID) {
      alert("Can we start, at least?");
     $.getJSON('sample.json', function(data) {
      alert("first success");
      $.each(data.person, function(i, xdata) {

Chrome affiche la première alerte, mais pas la seconde.

Toutes les idées?

Était-ce utile?

La solution

You can use the built in error function to display the error and debug it.

$(document).ready(function(){ // Make sure your jQuery is inside this
$.getJSON("sample.json", function(data) {
    alert('Point 1 Reached.');
    console.log(data); // Here we log to our console
    $.each(data.feed.entry, function(i, item) {
             // Do your stuff here
        }); // End of $.each

// Here Success, Completed & Error do something. chained onto $.getJSON
        }).success(function() { alert("success"); })
          .error(function(jqXHR, textStatus, errorThrown) { // Debug the error!!
                    console.log("error " + textStatus);
                    console.log("error throw " + errorThrown);
                    console.log("incoming Text " + jqXHR.responseText);
                }) // End of .error
          .complete(function() { alert("complete"); });
        });
}); // End of DOM Ready

This should show you the error in the console window of firefox or chrome (console.log will not work in IE and break the script). To bring up the console window in firefox or chrome press F12. If the JSON is not working, it will show an error which you can debug.


Update
Also make sure that this code is in your $(document).ready(). The $.getJSON() might cause unexpected behaviours across browsers if using it otherways.

Autres conseils

Is this running on a web server or are you just opening the file in a browser? If you are just opening the file you will have problems.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top