带有 JQuery 的 Ajax:200 可以,但不是“成功”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4840825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax with JQuery: 200 ok, but not "success"
提问by tkm256
I'm trying to use AJAX to send a query to Google Books and display the results on my website. I'm using JQuery to send the request and handling the response, like so:
我正在尝试使用 AJAX 向 Google 图书发送查询并在我的网站上显示结果。我正在使用 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. 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. Does anyone know why?
目前,如果收到响应,我只会让脚本提示“成功”。如果我使用我的脚本将该查询发送到本地页面进行测试,则效果很好。但是,当我按照 Developer API 页面上的说明将 URL 设置为上面列出的 Google 时,我从未看到警报。根据 Firebug 的说法,我收到了响应和 200 ok 的状态,但它没有进入“成功”路径。有谁知道为什么?
Edit: I should add that if I follow the URL directly, to http://books.google.cometc. with some random q, it displays the feed XML with no problems, so the query is not the issue.
编辑:我应该补充一点,如果我直接按照 URL 访问http://books.google.com等,并使用一些随机 q,它会毫无问题地显示提要 XML,因此查询不是问题。
采纳答案by Jason LeBrun
You can't make cross-domain requests using XMLHttpRequest under the standard browser security settings. 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.
在标准浏览器安全设置下,您不能使用 XMLHttpRequest 进行跨域请求。一种可能的解决方案是编写一个本地代理函数(假设您可以创建服务器端代码)将查询转发到外部站点,然后返回响应。
Edit: It looks like Google provides a JavaScript API as well. I would assume that they've crafted in such a way to avoid the cross-domain XHR issue.
编辑:看起来谷歌也提供了一个 JavaScript API。我会假设他们以这种方式精心制作以避免跨域 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. 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
编辑:书籍的 JavaScript API 已弃用。虽然它不再实用,但您可以通过 Wayback Machine 存档查看原始参考文档文本:http://web.archive.org/web/20120414070427/http: //code.google.com/apis/books/docs /js/devguide.html#execute
回答by lomanf
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 调用。
Docs here:
文档在这里:
http://code.google.com/apis/books/docs/js/jsondevguide.html#basic_query
http://code.google.com/apis/books/docs/js/jsondevguide.html#basic_query
jQuery example
jQuery 示例
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!
再见!