2009-08-05 4 views
9

ho questo codice, e anche provato qualcosa di simile utilizzando la funzione di $ GetJSON:utilizzando jQuery per ottenere i dati JSON restituisce errore di etichetta non valida

jQuery(document).ready(function(){ 
    var kiva_url = "http://api.kivaws.org/v1/loans/newest.json"; 

    jQuery.ajax({ 
     type: "GET", 
     url: kiva_url, 
      data:"format=json", 
     success: function(data){ 
      alert("here"); 
      jQuery.each(data.loans, function(i, loan){ 
       jQuery("#inner_div").append(loan.name + "<br />"); 
      }); 
     }, 
     dataType: "jsonp", 
     error: function(){ 
      alert("error"); 
     } 
    }); 

}); 

Quando guardo in Firebug si restituisce un "" etichetta non valida "errore. Ho cercato in giro un po 'di persone che si riferiscono all'utilizzo di un parser per analizzare i risultati. Posso vedere i risultati tornare in Firebug. Qualcuno può indicare un esempio di ciò che dovrei fare? Errore di

The Firebug:

invalid label http://api.kivaws.org/v1/loans/newest.json?callback=jsonp1249440194660&_=1249440194924&format=json& Line 1

uscita Esempio di ciò che il JSON appare come può essere trovato qui: http://build.kiva.org/docs/data/loans

risposta

5

Beh ho trovato la risposta ... Sembra Kiva non supporta jsonp che è ciò che jQuery sta facendo qui -

http://groups.google.com/group/build-kiva/browse_thread/thread/9e9f9d5df821ff8c

...we don't have plans to support JSONP. Supporting this advocates poor security practices and there are already some good ways to access the data from JavaScript that protect your application and your users. Here's a great article on the subject:

http://yuiblog.com/blog/2007/04/10/json-and-browser-security/

While the risk to Kiva lenders is low now since we are only dealing with public data, allowing private lender data to be imported via script tags is a risk further down the road. Our thought is the risk (and complexity added to create secure applications) is not worth the benefit to developers.

Writing a server-side proxy for the feeds you need is the most common solution to accessing data in browser-based applications. Some other tricks exist using iFrames. The best hope is the new breed of client- based technologies/standards that will let browser-based JavaScript access cross-domain resources securely ( http://dev.w3.org/2006/waf/access-control/ http://json.org/JSONRequest.html ). Some tools like BrowserPlus and Gears let you play with these today, but you won't be able to depend on these in the wild for a while.

As a final note, I'll point out that anyone using JSON responses in JavaScript should either parse JSON explicitly or validate the JSON before taking eval() to it. See here:

http://www.JSON.org/js.html

Linked from the page is a great reference implementation of the proposed ECMAScript JSON parser interface, JSON.parse().

Cheers, skylar

+0

Grazie Brendan, mi hai appena salvato un sacco di tempo! – ezeedub

0

Dove si verifica l'errore? Si verifica un errore quando si tenta di eseguire il loop dei dati Ajax e di aggiungerlo a inner_div? Se sì, per favore mostraci come sono i data.loans.

Inoltre, v'è un errore di battitura nel codice:

  jQuery.each(data.loans, function(i, loan){ 
        jQuery("#inner_div").append(loan.name + "<br />"); //It should be loan.name and not laon.name 
      }); 
    }, 
+0

Fisso il mio errore di battitura - credo che sia sulla retrieve- Se prendo quel ciclo jQuery ho ancora ottenere l'errore. – brendan

+0

quale riga dà l'errore? Puoi includere l'istantanea di errore Firebug? – SolutionYogi

+0

aggiunto output console firebug – brendan

2

Quando ritorni i dati, stai tornando con il tipo di contenuto corretto e come un metodo?

Si dovrebbe restituire i dati come segue (PHP 5 esempio):

$return = "my_callback_method(" . json_encode(array('data'=>'your data etc')). ")"; 

while (@ob_end_clean()); 
header('Cache-Control: no-cache'); 
header('Content-type: application/json'); 
print_r($return); 

Nella vostra vocazione codice javascript, è necessario disporre di un metodo in base al metodo di callback tuo ritorno, in questo caso:

function my_callback_method(returned_data){ 
} 

Così, il vostro js di chiamata completo dovrebbe essere simile alla seguente

jQuery(document).ready(function(){ 
var kiva_url = "http://api.kivaws.org/v1/loans/newest.json"; 

jQuery.ajax({ 
    type: "GET", 
    url: kiva_url, 
     data:"format=json", 
    dataType: "jsonp", 
    error: function(xmlhttp,error_msg){ 
      alert("error"+error_msg); 
    } 
}); 

function my_callback_method(data){ 
    alert("here"); 
    if(data && typeof(data) == 'object')){ 
    jQuery.each(data.loans, function(i, loan){ 
     jQuery("#inner_div").append(loan.name + "<br />"); 
    }); 
    } 
} 

}); 
0

Questa è la risposta http://forum.jquery.com/topic/jquery-getjson-invalid-label

Basta avvolgere la risposta Json con la richiesta di richiamata E.g. jQuery16203473509402899789_1315368234762({"Code":200,"Message":"Place added successfully","Content":""}); dove jQuery16203473509402899789_1315368234762 è la vostra richiesta di richiamata (si può ottenere tramite querystring) {"Code":200,"Message":"Place added successfully"} è la vostra risposta JSON