简体   繁体   English

使用JQuery的Ajax:200 ok,但不是“成功”

[英]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调用。

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! 再见!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery ajax GET请求,响应状态为200 OK时不触发成功 - JQuery ajax GET request, success not firing while response status is 200 OK Ajax 请求返回 200 OK,但触发了错误事件而不是成功 - Ajax request returns 200 OK, but an error event is fired instead of success AJAX 响应获取附加图像成功不工作返回 200 OK - AJAX response fetch additional images success not working return 200 OK Rails完成200 OK,但是$ .ajax()调用“错误”回调而不是“成功” - Rails completed 200 OK but $.ajax() calling 'error' callback instead of 'success' jQuery AJAX状态为“ 200 OK”,但无数据响应 - jQuery AJAX status “200 OK”, but no data response jQuery AJAX在200成功响应上失败404 - jQuery AJAX failing with 404 on a 200 success response jQuery ajax()返回200但未运行成功函数 - JQuery ajax() returning 200 but not running success function jquery ajax在直接执行时返回成功,但在附加到按钮时返回错误,即使服务器响应为200 OK - jquery ajax returns success when directly executed, but returns error when attached to button, even though server response is 200 OK jquery ajax调用使用readystate 4,状态200,statustext ok返回错误 - jquery ajax call returning an error with readystate 4, status 200, statustext ok jQuery $ .ajax无声地失败,没有错误消息,服务器响应200 OK - jQuery $.ajax failing silently, no error messages, and server responded with 200 OK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM