使用jQuery的外部API GET()请求 [英] External API GET() request using jQuery

查看:69
本文介绍了使用jQuery的外部API GET()请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用位于此处的 IMDb API v2.0 ,因此我决定对其进行测试.我不行我认为这是由于来自外部站点的跨浏览器AJAX请求..但是我不知道其他任何方式.例如,这是 imdbapi头像

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.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>

我正在使用Google Chrome.

I am using Google Chrome.

这对我有用:

    <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>

推荐答案

替换:

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

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

使用

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

这篇关于使用jQuery的外部API GET()请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆