[英]Ajax with JQuery: 200 ok, but not “success”
I'm trying to use AJAX to send a query to Google Books and display the results on my website. 我正在尝试使用AJAX向Google图书发送查询并在我的网站上显示结果。 I'm using JQuery to send the request and handling the response, like so: 我正在使用JQuery发送请求并处理响应,如下所示:
var query = [formatted input from a form];
var URL = "http://books.google.com/books/feeds/volumes?q="+query+"&start-index=1&max-results=5";
$.ajax({
type: "GET",
url: URL,
dataType: "xml",
success: function(data, status){
alert(status);
}
});
Currently, I just have the script alerting "success" if a response is received. 目前,如果收到回复,我只是让脚本提醒“成功”。 If I use my script to send that query to a local page for testing, this works just fine. 如果我使用我的脚本将该查询发送到本地页面进行测试,这样就可以了。 But when I set the URL to the Google one listed above, as instructed on the Developer API page, I never see the alert. 但是,当我按照开发人员API页面上的说明将URL设置为上面列出的Google时,我从未看到警报。 According to Firebug, I am receiving a response and a status of 200 ok as I should, but it's not getting to that "success" path. 根据Firebug的说法,我收到了一个响应和200个确定的状态,但是它没有达到那个“成功”的道路。 Does anyone know why? 有谁知道为什么?
Edit: I should add that if I follow the URL directly, to http://books.google.com etc. with some random q, it displays the feed XML with no problems, so the query is not the issue. 编辑:我应该补充一点,如果我直接关注网址, http://books.google.com等随机q,它会显示Feed XML没有问题,所以查询不是问题。
You can't make cross-domain requests using XMLHttpRequest under the standard browser security settings. 您无法在标准浏览器安全设置下使用XMLHttpRequest发出跨域请求。 One possible solution is to write a local proxy function (assuming you can create server-side code) that forwards the query to the external site, and then returns the response. 一种可能的解决方案是编写本地代理函数(假设您可以创建服务器端代码),将查询转发到外部站点,然后返回响应。
Edit : It looks like Google provides a JavaScript API as well. 编辑 :Google似乎也提供了JavaScript API。 I would assume that they've crafted in such a way to avoid the cross-domain XHR issue. 我认为他们是以避免跨域XHR问题的方式精心设计的。
http://code.google.com/apis/books/docs/js/devguide.html#execute http://code.google.com/apis/books/docs/js/devguide.html#execute
Edit : The JavaScript API for books was deprecated. 编辑 :不推荐使用书籍的JavaScript API。 While it's no longer practically useful, you can see the original referenced documentation text via the Wayback Machine archive: http://web.archive.org/web/20120414070427/http://code.google.com/apis/books/docs/js/devguide.html#execute 虽然它不再具有实用性,但您可以通过Wayback Machine存档查看原始参考文档文本: http ://web.archive.org/web/20120414070427/http: //code.google.com/apis/books/docs /js/devguide.html#execute
It's a cross-domain problem with ajax calls because browsers have a security model based on a domain policy. 这是ajax调用的跨域问题,因为浏览器具有基于域策略的安全模型。
if you don't wan to include the whole Google Books API, you can also use Google Ajax API with jsonp for cross-domain ajax calls. 如果您不想包含整个Google Books API,您还可以使用带有jsonp的Google Ajax API进行跨域ajax调用。
http://code.google.com/apis/books/docs/js/jsondevguide.html#basic_query http://code.google.com/apis/books/docs/js/jsondevguide.html#basic_query
var query = 'jquery';
var URL = 'https://ajax.googleapis.com/ajax/services/search/books?v=1.0&q=' + query;
$.ajax({
type: 'GET',
url: URL,
dataType: 'jsonp',
success: function( data, status ){
alert( data.responseData.results.length + ' results found!' );
},
error: function() {
alert( 'Something goes wrong!' );
}
});
Ciao! 再见!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.