Javascript jQuery/JSON无法调用API
我正在尝试使用jQuery使用Glosbe.com/a-api上的web服务。有人能告诉我为什么我下面的代码没有返回任何结果吗?我想用一个词在Glosbe上查询API,并在下面显示该词的定义 谢谢 这是我的jQuery:Javascript jQuery/JSON无法调用API,javascript,jquery,json,api,Javascript,Jquery,Json,Api,我正在尝试使用jQuery使用Glosbe.com/a-api上的web服务。有人能告诉我为什么我下面的代码没有返回任何结果吗?我想用一个词在Glosbe上查询API,并在下面显示该词的定义 谢谢 这是我的jQuery: $(document).ready(function(){ $('#term').focus(function(){ var full = $("#poster").has("img").length ? true : false; if(full === false
$(document).ready(function(){
$('#term').focus(function(){
var full = $("#poster").has("img").length ? true : false;
if(full === false){
$('#poster').empty();
}
});
var getPoster = function(){
var film = $('#term').val();
if(film === ''){
$('#poster').html("<h2 class='loading'>Ha! We haven't forgotten to validate the form! Please enter something.</h2>");
} else {
$('#poster').html("<h2 class='loading'>Your definition is on its way!</h2>");
$.getJSON("http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=hello&pretty=true" + film + "?callback=?", function(json) {
if (json !== "Nothing found."){
$('#poster').html('<h2 class="loading">Well, gee whiz! We found you a definition, skip!</h2><img id="thePoster" src=' + json[0].posters[0].image.url + ' />');
} else {
$.getJSON("http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=hello&pretty=true" + "?callback=?", function(json) {
console.log(json);
$('#poster').html('<h2 class="loading">Nothing found.</h2><img id="thePoster" src=' + json[0].posters[0].image.url + ' />');
});
}
});
}
return false;
};
$('#search').click(getPoster);
$('#term').keyup(function(event){
if(event.keyCode === 13){
getPoster();
}
});
});
$(文档).ready(函数(){
$('#term')。焦点(函数(){
var full=$(“#poster”).has(“img”).length?true:false;
如果(完整===错误){
$('海报').empty();
}
});
var getPoster=function(){
var film=$('#term').val();
如果(胶片==''){
$('#poster').html(“哈!我们没有忘记验证表单!请输入一些内容。”);
}否则{
$(“#poster”).html(“您的定义正在进行中!”);
$.getJSON(“http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=hello&pretty=true“+film+”?回调=?”,函数(json){
如果(json!=“未找到任何内容”){
$(“#poster”).html(‘哦,天哪!我们找到了定义,跳过!’);
}否则{
$.getJSON(“http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=hello&pretty=true“+”?回调=?”,函数(json){
log(json);
$(“#poster”).html('未找到任何内容');
});
}
});
}
返回false;
};
$(“#搜索”)。单击(getPoster);
$('#term').keyup(函数(事件){
如果(event.keyCode===13){
getPoster();
}
});
});
以及HTML:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="author" content="Matthew Hughes">
<meta name="Dictionary" content="A dictionary web service">
<title>Dictionary Web Application</title>
<script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.6.2.min.js"></script>
<!--jQuery, linked from a CDN-->
<script src="dictionary.js"></script>
<script type="text/javascript" src="http://use.typekit.com/oya4cmx.js"></script>
<script type="text/javascript">try{Typekit.load();}catch(e){}</script>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="container">
<header>
<h1>Dictionary Application</h1>
</header>
<section id="fetch">
<input type="text" placeholder="Enter a word..." id="term" />
<button id="search">Define!</button>
</section>
<section id="poster">
</section>
<footer>
<p>Created by Matthew Hughes</p>
</footer>
</div>
</body>
字典Web应用程序
尝试{Typekit.load();}catch(e){}
词典应用
定义
由马修·休斯创作
谢谢。因为这是一个跨域呼叫。浏览器将阻止它,因为同源策略。
如果您正在尝试使用跨域ajax,那么应该在ajax中使用CORS。服务器也应该启用它。跨域ajax的另一种方法是使用jsonp 如果您尝试发出请求,您将收到
400错误请求
错误。查看您的代码的URL:
http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase=hello&pretty=true" + film + "?callback=?"
您输入了一个错误,正确的URL应该是
http://glosbe.com/gapi/translate?from=eng&dest=eng&format=json&phrase="+ film + "&pretty=true&callback=?"
jQuery自动检测“callback=?”并切换到JSONP格式。这对我来说很好。但是请注意,响应没有
海报
字段。您正在尝试跨域请求。这样行吗?或者我仍然会有跨域请求的问题?有什么简单的方法吗?很好用!清理JSON输出的最简单方法是什么?你能告诉我我做错了什么吗?@Spencer你刚刚做了错误的串联,film
变量的值被附加到pretty=true
之后。清理JSON输出是什么意思?这很有道理,谢谢你的解释。目前,我从Glosbe收到的JSOn输出只是一大块JSOn?有什么方法可以清理它并使其输出更具可读性吗?@Spencer好吧,这取决于你自己如何解析它。你能解释一下json的作用吗。