use*_*594 7 javascript safari ajax jquery
我注意到Safari 5.0.5(6533.21.1)似乎正在提交重复的ajax调用.当我运行以下简化测试用例时:
// jquery 1.6 include
$(document).ready(function() {
setTimeout(function(e) {
var req1 = $.getJSON('/api/private/customers.json');
console.log('req1 sent');
}, 2000);
setTimeout(function(e) {
var req2 = $.getJSON('/api/private/customers.json');
console.log('req1 sent');
}, 4000);
});
Run Code Online (Sandbox Code Playgroud)
Safari资源面板和控制台显示两个xhr请求,但我的服务器日志显示三个xhr请求:
XX.XX.XX.XXX - - [10/May/2011:16:50:40 -0400] "GET /api/private/customers.json HTTP/1.1" 200 183 "https://sub.mydomain.com/customers" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"
XX.XX.XX.XXX - - [10/May/2011:16:50:42 -0400] "GET /api/private/customers.json HTTP/1.1" 200 183 "https://sub.mydomain.com/customers" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"
XX.XX.XX.XXX - - [10/May/2011:16:50:42 -0400] "GET /api/private/customers.json HTTP/1.1" 200 183 "https://sub.mycomain.com/customers" "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_7; en-us) AppleWebKit/533.21.1 (KHTML, like Gecko) Version/5.0.5 Safari/533.21.1"
Run Code Online (Sandbox Code Playgroud)
当我使用最新版本的Firefox发出相同的请求时,我正确地收到了两个请求:
XX.XX.XX.XXX - - [10/May/2011:16:52:00 -0400] "GET /api/private/customers.json HTTP/1.1" 200 183 "https://sub.mycomain.com/customers" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
XX.XX.XX.XXX - - [10/May/2011:16:52:02 -0400] "GET /api/private/customers.json HTTP/1.1" 200 183 "https://sub.mycomain.com/customers" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0.1) Gecko/20100101 Firefox/4.0.1"
Run Code Online (Sandbox Code Playgroud)
第一个请求似乎没有发生此行为,但所有后续请求都是一式两份发送的.有谁知道发生了什么事?在js中检测额外请求的努力是徒劳的.
小智 5
我想我们不会知道这是错误还是功能...无论如何,Safari(如10.0.2版本)仍然按照Dan Manastireanu的说明执行条件请求。我发现请求没有重复的方式是使用当前请求时间设置标头。(或者,您也可以在url中添加一个timestamp参数,如Dan Manastireanu再次提到的那样)'If-Unmodified-Since'
我拒绝了,setRequestHeader('Connection', "close")因为这是一个禁止的标题名称,并抛出Refused to set unsafe header "Connection"错误。
因此,基本请求如下所示:
var get = function(url, callbackSucess, callbackError) {
var request = new XMLHttpRequest();
request.open('GET', url, true);
request.timeout = 10000;
var now = new Date().getTime();
request.setRequestHeader('If-Unmodified-Since', now);
request.onreadystatechange = function() {
if (request.readyState !== 4) {
return;
}
if (request.status === 200) {
callbackSucess(request.status);
} else {
callbackError(request.status);
}
};
request.onerror = function (error) {
callbackError(error);
};
request.ontimeout = function () {
request.abort();
};
request.send(null);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3456 次 |
| 最近记录: |