2010-03-23 3 views
2

Je le fichier de code JSON suivant nommé: sections.jsonjQuery avec JSON

{ 
    "section1": { "priority": 1, "html": '<img src="../images/google.png" />'}, 
    "section2": { "priority": 2, "html": '<input type="button" value="Login" />'}, 
    "section3": { "priority": 3, "html": '<div>Some text</div>'}, 
    "section4": { "priority": 4, "html": '<div>Some text</div>'}, 
    "section5": { "priority": 5, "html": '<select><option>option1</option> <option>option2</option></select>'} 
} 

J'essaie dans votre code jquery mais alerte ne fonctionne pas

$.getJSON("sections.json", function(json) { 
    alert('h'); 
}); 
+1

Le chemin d'accès au fichier est-il correct? – rahul

+0

Que signifie "ne fonctionne pas"? Qu'est-ce qui se passe? Que faire si vous examinez le DOM et toutes les demandes XHR en utilisant Firebug? – Quentin

Répondre

1

Votre JSON devrait ressembler à ceci:

{ 
    "section1": { "priority": 1, "html": "<img src='../images/google.png' />"}, 
    "section2": { "priority": 2, "html": "<input type='button' value='Login' />"}, 
    "section3": { "priority": 3, "html": "<div>Some text</div>"}, 
    "section4": { "priority": 4, "html": "<div>Some text</div>"}, 
    "section5": { "priority": 5, "html": "<select><option>option1</option> <option>option2</option></select>"} 
} 

Les valeurs doivent être JSON, les guillemets simples ne suffiront pas :) A partir de jQuery 1.4, JSON invalide n'est plus autorisé par défaut (ils ont ajouté des vérifications supplémentaires pour s'assurer de sa validité, et le JSON dans votre question est bloqué par ceux :)