Javascript同步XMLHttpRequest的替代方案(作为Safari中的超时) [英] Alternatives for Javascript Synchronous XMLHttpRequest (as timing out in Safari)

查看:1009
本文介绍了Javascript同步XMLHttpRequest的替代方案(作为Safari中的超时)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短版本:
正在寻找等待Asynch XHR请求的方法或让Synch XHR在Safari中运行的方法。

Short version: Looking for a method to wait for an Asynch XHR request or a way to get Synch XHR working in Safari.

版本更长:
我正在开发一个使用各种外部数据源的Web服务。我正在使用Javacript前端,它对我的​​PHP服务器代码进行AJAX / XHR调用。没有跨站点问题,因为客户端只从我的服务器请求数据(服务器发出外部数据请求)。

Much longer version: I'm working on a web service that uses various external data sources mashed together. I am using a Javacript front-end which makes AJAX/XHR calls to my PHP server code. There's no cross-site issues as the client only requests data from my server (the server makes the external data requests).

我正在使用同步XHR请求在数据显示在屏幕上之前对数据进行一些后加载处理(排序,过滤等)。

I'm using a synchronous XHR request as I have some post-load processing (sorting, filtering, etc) to do on the data before it is presented on screen.

这一切都适用于IE,FF和Opera,但似乎是Safari的问题(我还没有尝试过Chrome)。

This all works fine on IE, FF and Opera, but seems to be a problem to Safari (I haven't tried Chrome yet).

在我的Windows机器上使用Firebug for Safari我可以看到服务器调用beng,然后在超过10秒后失败。在我的iPad上,消息略微更好,因为它说:NETWORK_ERR:XMLHttpRequest异常101:在同步模式下发生网络错误。

Using Firebug for Safari on my Windows machine I can see the server calling beng made and then it fails after a time above 10 seconds. On my iPad the message is slightly beter as it says: NETWORK_ERR: XMLHttpRequest Exception 101: A network error occurred in synchronous mode.

一些研究表明Safari将超时在同步模式下10秒后。似乎有一个超时功能,您可以使用该超时功能来扩展此功能(Safari的最大限制为60秒)。不幸的是我无法使用它。

A bit of research indicates that Safari will timeout after 10 seconds in synch mode. There appears to be a timeout function which timeout function you can use to extend this (with a max limit for Safari of 60 seconds). Unfortunately I can't get this to work.

我现在想知道人们会建议通过更改客户端Javacript代码来解决这个问题的最佳方法。

I'm now wondering what people would suggest as the best way of working around this through changes to the client Javacript code.

我正在考虑的两个选项是(i)找到Safari浏览器将遵循的同步XHR超时的工作示例;或者(ii)在异步XHR调用周围使用某种包装器,以便后加载处理首先等待加载。

The two options I'm thinking are (i) find a working example of a synch XHR timeout that Safari browsers will honour; or (ii) having some sort of wrapper around an asynch XHR call so that the post-load processing waits for the load first.

我不是特有的经验Javascript黑客,但我在这个项目上设置了相当数量。我没有使用过JQuery或任何其他框架,并且更愿意继续使用原始JS,以避免必须学习其他语法。 [你可以从我以前的帖子中看到我过去曾尝试过使用JQM和Spry,但两者都被证明是错误的选择,至少在现阶段已经被淘汰了。)

I'm not a particulalry experienced Javascript hacker, but I have setup a fair amount on this project. I haven't used JQuery or any other frameworks and would prefer to keep with raw JS just to avoid having to learn the additional syntax. [You may see from my previous posts I tried using JQM and Spry in the past, but both proved to be the wrong choices, at least at this stage and have since been ditched for now].

我觉得回调可能是正确的等待asych选项,但我不确定它是如何工作的或如何编码它。

I get the feeling a callback may be the right wait-for-asych option, but I'm not sure how this works or how you would code it.

这只是现阶段的原型,因此可以接受肮脏的黑客攻击。一旦我证明了功能,就可以在卡片上进行完全重写。

This is just a prototype at this stage, so dirty hacks are acceptable. A full re-write is already on the cards for later on once I have proven functionality.

感谢大家对此的想法和建议。

Appreciate peoples thoughts and advice on this.

问候,
Pete。

Regards, Pete.

推荐答案

一般来说,你会想要坚持异步请求,因为它们是非阻塞的。对于它们,你会想要使用一个回调 - 或者简单地说,一个设置为稍后调用的函数。

Generally, you'll want to stick to asynchronous requests, as they're non-blocking. And with them, you'll want to use a callback -- or, simply, a function that is setup to be called later.

你可以设置回调函数 onreadystatechange 属性 XMLHttpRequest

You can set the callback with the onreadystatechange property of an XMLHttpRequest:

xhr.onreadystatechange = function () {
    if (xhr.readyState === 4) {   // DONE
        if (xhr.status === 200) { // OK
            handleSuccess(xhr);
        } else {
            handleError(xhr);
        }
    }
};

如属性名称所示,它将被称为 readyState的值更改,其中值 4 表示请求已完成(成功与否)。

As the property name suggests, it will be called as the value of readyState changes, where a value of 4 means the request has completed (successful or not).

然后你将在另一个函数中处理你的排序,过滤等 - 在这个例子中, handleSuccess

You'd then handle your sorting, filtering, etc. within another function -- in this example, handleSuccess.

您还可以使用任何现有库中的任何一个 - 例如, jQuery (1.6或更高版本)这个片段):

You can also benefit from using any of a number of existing libraries -- e.g., jQuery (1.6 or later for this snippet):

$.get('/web/service/request')
    .done(function (result) {
        // sorting, filtering, etc
    })
    .fail(function (xhr) {
        // error notification, etc.
    });

这篇关于Javascript同步XMLHttpRequest的替代方案(作为Safari中的超时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆