【问题标题】:Jquery PHP ajax show server response as it send itJquery PHP ajax 在发送时显示服务器响应
【发布时间】:2014-06-11 12:27:57
【问题描述】:

我想知道是否有可能显示服务器响应,因为它在一个耗时的请求中实时发送。

上下文:

1) 一个 Jquery 函数向服务器发送一个 ajax 请求并打开一个 Dialog 小部件等待响应以更新 Dialogs 内容。

2) PHP 服务器处理请求的过程非常耗时,20 秒甚至更多分钟,这是一个用户知道丁字裤需要时间,需要管理很多东西的内网网站。

当服务器工作时,我的 PHP 代码中有几个“回声”,有一些话要说,而服务器仍在处理其他事情以“回声”更多。

我的问题是只要服务器还没有完成,对话框就不会更新。

我想在浏览器接收到部分响应后立即实时显示部分响应,并使用来自独特函数的 SINGLE Ajax 调用。

所以...其实有两个问题:

我的浏览器是否在实时接收“回声”?

如果是:我如何现场展示?

如果否:如何实时发送?以及如何现场直播?

任何帮助将不胜感激。

谢谢

【问题讨论】:

  • 您需要查看 websocket,因为无状态请求(例如 AJAX)不会执行您想要的操作。
  • 我总是制作多个与反馈系统一起工作的 ajax 函数。函数 1 去服务器告诉它,我很快就会发送数据给用户反馈(如加载 gif),函数 2 然后进行计算,函数 3 等待函数 2 完成,然后从函数 2 获取结果.

标签: javascript php jquery ajax


【解决方案1】:

我的浏览器是否在实时接收“回声”?

不,ajax 等待 php 完成,然后立即抛出所有回显。

如果否:如何实时发送?以及如何现场直播?

我不知道如何快速直播,但有一些解决方法。

创建多个将与服务器交互的 ajax 函数,使其看起来像是实时的。就像我在 cmets 中所说的那样。有了这个,我假设你知道 ajax 是如何工作的。

//javascript
function f1(){
    //call ajax to tell the server send a start response
}

//php
echo "<img src='loading.gif'>";
//or something like it.

//ajax function to make the server do the data.
function f2(){
    //call ajax to tell the server to start and save data in a session
    //Or something like it also set div to be changed to a hidden div.
}

//php
//run the required script. then echo a finished response that can be found by js.
//to make it seem extra live you can split this up in different parts.
//so you can show progress to the client side. like step 1 of 5 complete.

//ajax function 3 for finished
function f3(){
    //tell server to echo results once f2 is finished;
}

//php
//echo results loaded from where you saved it.
//probably session, delete the session when finished.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2012-07-11
    • 2020-07-25
    • 2012-02-26
    • 1970-01-01
    • 2012-04-24
    • 1970-01-01
    相关资源
    最近更新 更多