2010-09-08 5 views
0

J'aime travailler avec JSON et jquery. Lorsque je test hors ligne de script sur localhost il fonctionne très bien, mais quand je change ce fichier JSON à un serveur, il doens't travailler al tousJSON avec php et Jquery

jQuery(function($) { 

    $.ajax 
    ({ 
     type: "GET", 
     url: "test.php", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: companySearchSuccess, 
     error: onError 
    }); 



function companySearchSuccess(json) 
{ 
    alert("stap1"); 
var select = $('#city-list'); 

       $.each(json, function(k, v) { 
         var option = $('<option />'); 

         option.attr('value', v) 
           .html(v) 
           .appendTo(select); 
       }); 

} 

function onError(m){ 
alert("Fout is: "+m); 
} 

}); 

Cela fonctionne bien,

mais celui-ci ne:

jQuery(function($) { 

    $.ajax 
    ({ 
     type: "GET", 
     url: "http://www.example.com/test.php", 
     contentType: "application/json; charset=utf-8", 
     dataType: "json", 
     success: companySearchSuccess, 
     error: onError 
    }); 



function companySearchSuccess(json) 
{ 
    alert("stap1"); 
var select = $('#city-list'); 

       $.each(json, function(k, v) { 
         var option = $('<option />'); 

         option.attr('value', v) 
           .html(v) 
           .appendTo(select); 
       }); 

} 

function onError(m){ 
alert("Fout is: "+m); 
} 

}); 

L'erreur est: objet XMLHttpRequest>

S'il vous plaît aidez-moi

Répondre

2

Si vous exécutez votre application sur localhost et tentez de pointer votre requête http xml vers un autre domaine, vous enfreignez la même règle d'origine et le navigateur ne la fera pas.

Je suis au courant de 2 options:

1) Utilisez jsonp. 2) Si vous contrôlez le domaine/serveur cible, dans votre configuration dev, faites correspondre le protocole, le domaine et le port. C'est le cas où vous pouvez déployer une version du serveur sur localhost.