Javascript 同步 XMLHttpRequest 的替代方案(在 Safari 中超时)

2023-05-14前端开发问题
18

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

问题描述

限时送ChatGPT账号..

短版:正在寻找等待异步 XHR 请求的方法或让同步 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 Exception 101: A network error occurred in synchronous mode.

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].

我觉得回调可能是正确的等待异步选项,但我不确定它是如何工作的,或者你将如何编码.

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.

问候,皮特.

推荐答案

通常,您会希望坚持使用异步请求,因为它们是非阻塞的.有了它们,您将需要使用回调 - 或者简单地说,是一个设置为稍后调用的函数.

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 中超时)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

ajax请求获取json数据并处理的实例代码
ajax请求获取json数据并处理的实例代码 $.ajax({ type: 'GET', url: 'https://localhost:44369/UserInfo/EditUserJson',//请求数据 data: json,//传递数据 //dataType:'json/text',//预计服务器返回的类型 timeout: 3000,//请求超时的时间 //回调函数传参 suc...
2024-11-22 前端开发问题
215

js删除数组中指定元素的5种方法
在JavaScript中,我们有多种方法可以删除数组中的指定元素。以下给出了5种常见的方法并提供了相应的代码示例: 1.使用splice()方法: let array = [0, 1, 2, 3, 4, 5];let index = array.indexOf(2);if (index -1) { array.splice(index, 1);}// array = [0,...
2024-11-22 前端开发问题
182

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

JavaScript(js)文件字符串中丢失"\"斜线的解决方法
问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转...
2024-10-17 前端开发问题
437

layui中table列表 增加属性 edit="date",不生效怎么办?
如果你想在 layui 的 table 列表中增加 edit=date 属性但不生效,可能是以下问题导致的: 1. 缺少日期组件的初始化 如果想在表格中使用日期组件,需要在页面中引入 layui 的日期组件,并初始化: script type="text/javascript" src="/layui/layui.js"/scrip...
2024-06-11 前端开发问题
455

Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript
Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)...
2024-04-20 前端开发问题
5