确定/规避AJAX请求是否超时的方法? [英] Way to determine/circumvent if an AJAX request timed out?

查看:86
本文介绍了确定/规避AJAX请求是否超时的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的网页(PHP,JS和HTML),显示该网页是为了说明计算正在进行中.此计算是由执行实际计算的PHP脚本的 pure JavaScript AJAX请求触发的.有关详细信息,请参阅此处

I have a simple web-page (PHP, JS and HTML) that is displayed to illustrate that a computation is in process. This computation is triggered by a pure JavaScript AJAX-request of a PHP-script doing the actual computations. For details, please see here

什么是实际的计算,并不起作用,因此,为简单起见,它只是一个sleep()命令.

What the actual computation is, does not play a role, so for simplicity, it is just a sleep()-command.

当我在本地执行相同的代码(浏览器在localhost下访问网站:linux,apache,php-mod)时,它工作正常,与睡眠时间无关.

When I execute the same code locally (browser calls website under localhost: linux, apache, php-mod) it works fine, independant of the sleep-time.

但是,当我让它在另一台机器上运行(不是本地主机,也没有Linux,apache,php-mod)时,PHP脚本确实运行了(创建了结果),但是AJAX请求没有得到任何响应,因此如果睡眠时间> = 900秒,则不会发生"onreadystatechange".当睡眠时间<900秒也可以正常工作,并且AJAX请求已正确终止(readyState == 4和status == 200).

However, when I let it run on a different machine (not localhost, but also Linux, apache, php-mod), the PHP-script does run through (results are created), but the AJAX-request does not get any response, so there is no "onreadystatechange" if the sleep-time is >= 900 seconds. When sleep-time < 900 seconds it also works nicely and the AJAX-request is correctly terminated (readyState==4 and status==200).

apache和php-configuration或多或少是默认的,我已经验证了那里的关键选项(max_execution_time等),但是这里似乎没有一个有效的选择,因为它们更短(<1分钟)或更大,例如为垃圾收集器(24分钟).

The apache and php-configuration are more or less default and I verified the crucial options there already (max_execution_time etc.) but none seems to be valid here as they are either shorter (<1 min.) or bigger, e.g. for the garbage-collector (24 min.).

所以我很困惑可能导致这种情况的原因.我认为这可能与网络有关,尽管我在路由器中找不到任何合适的选择.apache-logs或PHP中也没有报告错误(错误loggin到文件).

So I am absolutely confused what may cause this. I am thinking it might be network-related, although I didn't find any appropriate option in my router or so. Also no error is reported in the apache-logs or in PHP (error loggin to file).

使用AJAX请求保留JavaScript会在成功返回时显示request.status,令人惊讶的是,当我在睡眠结束后在浏览器窗口中按"Esc"时,也会显示状态"200",但不会自动显示为它应该这样做.

Letting the JavaScript with the AJAX-request display the request.status upon successfull return, surprisingly when I hit "Esc" in the browser window after the sleep is over, I also get the status "200" displayed but not automatically as it should do it.

无论如何,我希望您可能对如何解决此问题有所了解?也许每10分钟左右在客户端和服务器之间进行一些虚拟通信可能会成功,但是我不知道如何最好地执行这样的操作,尤其是让它对用户透明并且不干扰实际工作进行计算/睡眠的过程.

In any case, I am hoping that you may have an idea how to circumvent this problem? Maybe some dummy-communication between client and server every 10 minutes or so might do the trick, but I don't have an idea how to best do something like this, especially letting this be transparent to the user and not interfering with the actual work of doing the computations/sleep.

最好

阴影

P.S.我所引用的帖子是我写的,但似乎在践踏一下它可能与某些config-option有关的想法,在这种情况下,不是.这就是为什么我在这里写这篇文章的原因,基本上是在寻求一种解决这种问题的方法,而不论其起源是什么.

P.S. The post that I am referencing is written by me, but seems to tramsit the idea that it might be related to some config-option, which seems not to be the case. This is why I am writing this post here, basically asking for a way to circumvent such an issue regardless of it's origin.

推荐答案

我来自您提到的其他帖子!

I'm from the other post you mentioned!

现在,我对您要执行的操作有了更多的了解:监视可能长时间运行的服务器作业,我可以推荐一些效果更好的东西,它不是您问题的直接答案,而是设计上的考虑从本质上讲,它包括一个更合适的解决方案.

Now that I know more about what you are trying to do: monitor a possibly long running server job, I can recommend something which should turn out a lot better, its not a direct answer to your question, but its a design consideration which includes by its nature a more suitable solution.

基本上,从监视其进度中取消链接启动"服务器端任务的操作.

Basically, unlink the actions of "starting" the server side task, from monitoring its progress.

  • execute.php在服务器上启动您的后台作业,并立即返回.
  • 另一个脚本/URL(可称为status.php)可用于检查execute.php正在执行的任务的进度.
    • 请求status.php时,除非有报告要报告,否则将不会返回,除非经过30秒(或其他固定时间),此时它将返回一个值,您知道这意味着再次检查".循环执行此操作,可以在后台任务完成时立即收到通知.

    有关类似于此方法的更多详细信息: http://billhiggins.us/blog/2011/04/27/resty-long-ops

    More details on an approach similar to this: http://billhiggins.us/blog/2011/04/27/resty-long-ops

    希望此帮助为您提供一些设计思路,以解决您的问题!

    I hope this help give you some design ideas to address your problem!

    这篇关于确定/规避AJAX请求是否超时的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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