gpt4 book ai didi

firefox - 对 Twitter 的 jQuery.ajax 调用成功,但对 Firefox 返回 null

转载 作者:行者123 更新时间:2023-12-03 22:33:56 26 4
gpt4 key购买 nike

我有代码使用 jQuery 的 Ajax 方法向 Twitter(搜索)发出简单的 get 请求。该代码在 Safari 上运行良好,但在 Firefox (3.6.3) 上失败。在 Firefox 的情况下,我的 jQuery.ajax 参数“成功”方法被调用,但提供的数据为空。 (在 Safari 中,我收到了大量 JSON 数据。)

我的 Ajax 调用是:

$.ajax({
url: 'http://search.twitter.com/search.json?q='+searchTerm,
dataType: 'json',
async: true,
beforeSend: function(request) {
window.console.log('starting AJAX request to get Twitter data');
},
success: function(data, textStatus, request) {
window.console.log('AJAX request to get Twitter succeeded: status=' + textStatus);
callback(data);
},
error: function(request, status, error) {
window.console.log('Ajax request to get user data --> Error: ' + status);
errback(request, status, error);
}
});

Firebug 显示响应 header :

Date    Sun, 11 Apr 2010 22:30:26 GMT
Server hi
Status 200 OK
X-Served-From b021
X-Runtime 0.23841
Content-Type application/json; charset=utf-8
X-Served-By sjc1o024.prod.twitter.com
X-Timeline-Cache-Hit Miss
Cache-Control max-age=15, must-revalidate, max-age=300
Expires Sun, 11 Apr 2010 22:35:26 GMT
Vary Accept-Encoding
X-Varnish 1827846877
Age 0
Via 1.1 varnish
X-Cache-Svr sjc1o024.prod.twitter.com
X-Cache MISS
Content-Encoding gzip
Content-Length 2126
Connection close

HTTP 状态为 OK (200),Content-Type 正确为 application/json,Content-Length 为 2126 (gzip'd) 意味着数据已返回。然而,Firebug显示响应为空,对提供的数据的测试显示它为“空”。

我知道 Stack Overflow 上有类似的帖子, jQuery $.get() function succeeds with 200 but returns no content in Firefox 由此可以假设这个问题可能与跨域安全有关,但是...我知道有很多 JavaScript 小部件以及 Ajax 从 Twitter 获取数据。我需要启用某些功能才能实现此操作吗?

最佳答案

您正在尝试进行跨域 Ajax 调用。为此,您需要使用 JSONP。

JQuery 理解 JSONP,它将为您处理所有底层技巧。

只需要添加参数&callback=?添加到您的 URL,JQuery 会将请求作为跨域调用。更重要的是,它会理解并处理来自服务器的 JSONP 响应,因此对您来说它将是透明的。

关于firefox - 对 Twitter 的 jQuery.ajax 调用成功,但对 Firefox 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2619002/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com