javascript - Richiesta GET() a un'API esterna usando jQuery
Sto usando l'API IMDb v2.0 situata qui e ho deciso di testarla. Non posso. Penso che sia a causa delle richieste AJAX cross-browser da siti esterni... ma non conosco nessun altro modo. Per esempio, ecco un test su imdbapi avatar
Vedi? Ecco il mio codice.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>IMDB api</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.18/jquery-ui.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$('#movie').keyup(function() {
var yourMovie = $("#movie").val();
$("#debug").append("You are searching for ... "+yourMovie+"\n");
dataString = "t=Avatar";
$.ajax({
type: "GET",
url: "http://www.imdbapi.com/",
cache: false,
data: dataString,
success: function(html){
//$("#more").after(html);
alert("Success!");
}
});
});
});
</script>
</head>
<body>
<form action="#" method="get" enctype="text/html" >
<input type="text" id="movie" maxlength="50" />
</form>
<div id="other">
Trigger the handler
</div>
<br />
<textarea id="debug" style="width: 500px;height:150px;border:1px solid black;font-face:typewriter;"></textarea><br />
<textarea id="more" style="width: 500px;height:150px;border:1px solid red;font-face:typewriter;"></textarea>
</body>
</html>
Sto usando Google Chrome.
Ecco cosa ha funzionato per me:
<script type="text/javascript">
$(document).ready(function()
{
$('#movie').keyup(function() {
var yourMovie = $("#movie").val();
$("#debug").append("You are searching for ... "+yourMovie+"\n");
dataString = "callback=?&t=Avatar";
$.getJSON('http://www.imdbapi.com/', dataString, function(html){
//$("#more").after(html);
alert("Success!");
});
});
});
</script>
Risposta n. 1
Sostituire :
$.ajax({
type: "GET",
url: "http://www.imdbapi.com/",
cache: false,
data: dataString,
success: function(html){
//$("#more").after(html);
alert("Success!");
}
});
Con
$.getJSON('http://www.imdbapi.com/?' + dataString, function(json_data){
alert(JSON.stringify(json_data));
});
Risposta n. 2
Si tratta di una chiamata AJAX cross domain, quindi hai bisogno di un callback. jQuery rende questo davvero facile, basta aggiungere ?callback=? al tuo URL.
url: "http://www.imdbapi.com/?" + dataString + "&callback=?"
Salta il data=dataString in questo caso, rende le cose più facili (lo trovo).
Prova questo e continua ulteriormente:
$.getJSON("http://www.imdbapi.com/?" + dataString + "&callback=?").success(function(data){
console.log(data); // will contain all data (and display it in console)
})
Questo è lo stesso di:
$.ajax({
type: "GET",
url: "http://www.imdbapi.com/?"+dataString+"&callback=?",
dataType: 'JSONP'
success: function(data){
console.log(data);
}
