Ajax 与 JQuery:200 正常,但不是“成功”

发布于 2024-10-15 02:45:37 字数 710 浏览 8 评论 0原文

我正在尝试使用 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);
    }
});

目前,我只有脚本在收到响应时发出“成功”警报。如果我使用脚本将该查询发送到本地页面进行测试,则效果很好。但是,当我按照开发人员 API 页面上的说明将 URL 设置为上面列出的 Google URL 时,我从未看到该警报。根据 Firebug 的说法,我收到了响应和状态 200 ok,这是我应该做的,但它没有到达“成功”路径。有谁知道为什么?

编辑:我应该补充一点,如果我直接访问该网址,则为 http://books.google.com 等。使用一些随机 q,它可以毫无问题地显示提要 XML,因此查询不是问题。

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:

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?

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

最丧也最甜 2024-10-22 02:45:37

在标准浏览器安全设置下,您无法使用 XMLHttpRequest 发出跨域请求。一种可能的解决方案是编写一个本地代理函数(假设您可以创建服务器端代码),将查询转发到外部站点,然后返回响应。

编辑:看起来 Google 也提供了 JavaScript API。我认为他们采用这种方式来避免跨域 XHR 问题。

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

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.

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.

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

温柔戏命师 2024-10-22 02:45:37

这是 ajax 调用的跨域问题,因为浏览器具有基于域策略的安全模型。

如果您不想包含整个 Google Books API,您还可以使用 Google Ajax API 和 jsonp 进行跨域 ajax 调用。

此处的文档:

http://code.google.com/apis /books/docs/js/jsondevguide.html#basic_query

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!

It's a cross-domain problem with ajax calls because browsers have a security model based on a domain policy.

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.

Docs here:

http://code.google.com/apis/books/docs/js/jsondevguide.html#basic_query

jQuery example

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文