本文介绍了Ajax和JQuery的:200行,但不与QUOT;成功"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用AJAX来我的网站上查询发送给谷歌图书并显示结果。我使用jQuery发送请求和处理响应,像这样:

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);
    }
});

目前,我只是有这个脚本警报成功,如果接收到响应。如果我用我的脚本来发送查询到本地页面进行测试,这工作得很好。但是,当我设置的URL谷歌上面列出的指示,对开发者API页面上,我从来没有看到警报。据Firebug的,我收到的回应和200 OK,我应该状态,但它没有得到以成功的道路。没有人知道为什么?

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?

编辑:我要补充一点,如果我直接跟在URL,以 http://books.google.com 等一些随机Q,它会显示XML饲料没有问题,所以该查询不是问题。

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.

推荐答案

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

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.

修改:它看起来像谷歌提供了一个JavaScript API,以及。我会认为他们已经制作以这样的方式,以避免跨域XHR问题。

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

修改:对于图书的JavaScript API是pcated德$ P $。虽然它不再实际有用的,你可以看到通过Wayback机器存档的原始参考文档的文本:<一href="http://web.archive.org/web/20120414070427/http://$c$c.google.com/apis/books/docs/js/devguide.html#execute" rel="nofollow">http://web.archive.org/web/20120414070427/http://$c$c.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

这篇关于Ajax和JQuery的:200行,但不与QUOT;成功&QUOT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 02:31