gpt4 book ai didi

jQuery .ajax 调用 bit.ly 在 IE 中返回结果,但在 FF 或 Chrome 中不返回结果

转载 作者:行者123 更新时间:2023-12-01 01:30:04 24 4
gpt4 key购买 nike

我正在尝试使用 jQuery 和 .ajax 调用来调用 bit.ly URL 缩短服务。

更新 我想知道这是否是跨域安全问题?我正在从 mysite.com 调用 bit.ly

<html><head>
<script type="text/javascript" src="http://www.twipler.com/settings/scripts/jquery.1.4.min.js"></script>
<script type="text/javascript">
jQuery.fn.shorten = function(url)
{
var resultUrl = url;

$.ajax(
{
url: "http://api.bit.ly/shorten?version=2.0.1&login=twipler&apiKey=R_4e618e42fadbb802cf95c6c2dbab3763&longUrl=" + url,
async: false,
dataType: 'json',
data: "",
type: "GET",
success:
function (json) { resultUrl = json.results[url].shortUrl; }
});

return resultUrl;
} ;
</script></head><body>
<a href="#"
onclick="alert($().shorten('http://amiconnectedtotheinternet.com'));">
Shorten</a> </body> </html>

这适用于 IE8,但不适用于 FireFox (3.5.9) 或 Chrome。在这两种情况下,“json”均为 null。

IE8 中的 header

GET http://api.bit.ly/shorten?ver..[SNIP]..dtotheinternet.com HTTP/1.1
Accept: application/json, text/javascript, */*
Accept-Language: en-US
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0;
SLCC2; .NET CLR 2.0.50727; Media Center PC 6.0; InfoPath.2;
.NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET CLR 3.0.30729)
Host: api.bit.ly
Connection: Keep-Alive

Chrome 中的 header

GET http://api.bit.ly/shorten?versio..[SNIP]..nectedtotheinternet.com HTTP/1.1
Host: api.bit.ly
Connection: keep-alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/532.5
(KHTML, like Gecko) Chrome/4.1.249.1045 Safari/532.5
Origin: file://
Accept: application/json, text/javascript, */*
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3

所以唯一明显的区别是 Chrome 正在发送“Origin: file://”,但我不知道如何阻止它这样做。

最佳答案

使用 Fiddler 验证请求的实际负载和 bit.ly 服务的响应。将 IE 请求/响应与 Chrome 请求/响应进行比较,找出不同之处。

我(疯狂)的猜测是,当 Firefox 和 Chrome 发送请求时,由于浏览器发出请求的方式不同,该服务会返回一条错误消息。特别是,您附加 url 参数的方式对我来说似乎有点可疑,为了以防万一,我会对它进行 url 编码。

更新: 所以 HTTP header 确实揭示了问题。 :-)

Origin header当用户代理想要建议网站该请求是跨源请求时添加。显然Chrome has added support最近对于这个标题。当然:

The details of the Origin header are still being finalized. We will update the implementation in Google Chrome as the specification evolves based on feedback from Mozilla and from the W3C and IETF communities at large.

您目前可能无法采取任何措施来阻止 Chrome 发送该 header 。顺便说一句,Origin header 似乎是 Firefox 3.6 首次引入的,我怀疑您是运行所有最新、最好的浏览器的人之一。 :-)

顺便说一句,XMLHttpRequest 确实有跨域限制。所以,我想知道 jQuery.Ajax 是否没有使用新的 XDomainRequest在 IE8 上而不是 XMLHttpRequest

但是回到您的问题 - 此时一切都表明唯一可用的解决方案是对您的站点进行 Ajax 调用并从您的服务器进行 bit.ly 调用。我知道这不是最佳选择...

关于jQuery .ajax 调用 bit.ly 在 IE 中返回结果,但在 FF 或 Chrome 中不返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2601315/

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