最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - External API GET() request using jQuery - Stack Overflow

programmeradmin3浏览0评论

I am using the IMDb API v2.0 located here and I decided to test it. I can't. I think it's beacuse of cross-browser AJAX request from external sites.. but I don't know any other way. For example, here's a test at imdbapi avatar

See? Here's my code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ".dtd">
<html xmlns="" 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=".7.1/jquery.min.js"></script>
    <script type="text/javascript" src=".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: "/",
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>

I am using Google Chrome.

Here's what worked for 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('/', dataString, function(html){
//$("#more").after(html);
alert("Success!");
});


});
});
</script>

I am using the IMDb API v2.0 located here and I decided to test it. I can't. I think it's beacuse of cross-browser AJAX request from external sites.. but I don't know any other way. For example, here's a test at imdbapi avatar

See? Here's my code.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3/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./ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis./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./",
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>

I am using Google Chrome.

Here's what worked for 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./', dataString, function(html){
//$("#more").after(html);
alert("Success!");
});


});
});
</script>
Share Improve this question edited Jan 16, 2013 at 8:33 Milind Anantwar 82.2k25 gold badges96 silver badges127 bronze badges asked Apr 2, 2012 at 22:24 testtest 18.2k67 gold badges173 silver badges245 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

Replace:

$.ajax({
type: "GET",
url: "http://www.imdbapi./",
cache: false,
data: dataString,

success: function(html){
//$("#more").after(html);
alert("Success!");
}
});

With

$.getJSON('http://www.imdbapi./?' + dataString, function(json_data){
alert(JSON.stringify(json_data));
});

It is a cross domain AJAX call, therefore you need a callback. jQuery makes this really easy, just add ?callback=? to your url.

url: "http://www.imdbapi./?" + dataString + "&callback=?"

Skip the data = dataString in this case, makes it easier (I find).

Try this, and continue on it further:

$.getJSON("http://www.imdbapi./?" + dataString + "&callback=?").success(function(data){
    console.log(data); // will contain all data (and display it in console)
})

This is the same as:

$.ajax({
type: "GET",
url: "http://www.imdbapi./?"+dataString+"&callback=?",
dataType: 'JSONP'
success: function(data){
    console.log(data);
}

You can run cross domain Ajax with jQuery. Set the crossDomain option at the call site:

http://api.jquery./jQuery.ajax/

crossDomain(added 1.5)Boolean Default: false for same-domain requests, true for cross-domain requests If you wish to force a crossDomain request (such as JSONP) on the same domain, set the value of crossDomain to true

Edit-

Actually, what exactly is your problem? I tried this and it returns Json properly.

http://jsfiddle/7VcUJ/

Response example:

HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: text/html; charset=utf-8 Expires: -1 Server: Microsoft-IIS/7.0 X-AspNet-Version: 4.0.30319 X-Powered-By: ASP.NET Date: Mon, 02 Apr 2012 22:28:14 GMT Content-Length: 618

{"Title":"Avatar","Year":"2009","Rated":"PG-13","Released":"18 Dec 2009","Genre":"Action, Adventure, Fantasy, Sci-Fi","Director":"James Cameron","Writer":"James Cameron","Actors":"Sam Worthington, Zoe Saldana, Sigourney Weaver, Michelle Rodriguez","Plot":"A paraplegic marine dispatched to the moon Pandora on a unique mission bees torn between following his orders and protecting the world he feels is his home.","Poster":"http://ia.media-imdb./images/M/MV5BMTYwOTEwNjAzMl5BMl5BanBnXkFtZTcwODc5MTUwMw@@._V1_SX320.jpg","Runtime":"2 hrs 42 mins","Rating":"8.1","Votes":"386930","ID":"tt0499549","Response":"True"}

Add a callback parameter to the URL to use JSONP:

dataString = "t=Avatar&callback=?";

Using a $ will cause jQuery to auto-generate a callback function for you and handle the response automatically.

Remended reading: What is JSONP all about?

发布评论

评论列表(0)

  1. 暂无评论