简体   繁体   English

如何使用JavaScript从PHP脚本交互式获取数据

[英]How to interactively get data from php script using javascript

I have this php script: 我有这个php脚本:

<?php
$arr = array(array("a","b"),array("c","d"));
qq($arr);
function qq($arr){
foreach($arr as $ar => $r){
//getting some work done
//sending the array $r (or values)to javascript
}
}
?>

Is it possible to get the value of 0 array using javascript before array 1. 是否可以在数组1之前使用javascript获得数组0的值。

what i get so far is this js from stackoverflow: 我到目前为止所得到的是来自stackoverflow的js:

<script type="text/javascript">
function q(){

var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "http://127.0.0.0.1/q.php");
xmlhttp.send(null);
xmlhttp.onreadystatechange = callbackFunction;

function callbackFunction(){
if (xmlhttp.readyState == 4){
xml = xmlhttp.responseText;
document.getElementById("q").value = xml;
}}}
</script>

but it's not doing what i want(i think i'm missing something). 但是它没有满足我的要求(我想我缺少了什么)。

my html: 我的html:

<input type="submit" onclick="q();" />
<div id="q"></div>
<div id="q1"></div>

Is it possible to put array 0 value in div(id=q) and when array 1 is ready put it's value in div(id=q1) 是否可以将数组0的值放在div(id = q)中,当数组1准备好时,将其值放在div(id = q1)中

The "normal" HTTP Requests via XMLHttpRequest() do not allow such behaviour in a single request. 通过XMLHttpRequest()的“常规” HTTP请求不允许在单个请求中出现这种行为。

You have the following options instead: 您可以使用以下选项:

Server Send Events 服务器发送事件

upside 上边

  • They are incredibly easy to build. 它们非常容易构建。 Nothing really special is needed on the server. 服务器上不需要任何特别的东西。 Just google for server sent events and you'll find some examples on how they work. 只是用google搜索服务器发送的事件,您会找到一些有关它们如何工作的示例。
  • They go through proxys and firewalls just as well as any other http request, because it is a normal http request. 它们像通过任何其他http请求一样,也要通过代理和防火墙,因为这是一个正常的http请求。

downside 缺点

  • Does not run on Internet Explorer (?) 无法在Internet Explorer(?)上运行
  • May fail to update in realtime if a proxy somewhere buffers too much. 如果某处的代理缓冲过多,可能无法实时更新。 However in this case it still works, it just misses the realtime communication 但是,在这种情况下它仍然有效,只是错过了实时通信

Websockets 的WebSockets

upside 上边

Works in all recent browsers. 在所有最新的浏览器中均可使用。 Enables you to do full duplex communication and gets everything right that you need. 使您能够进行全双工通信并正确处理所需的一切。

downside 缺点

  • May or may not work through proxies depending on their configuration. 可能会或可能不会通过代理工作,具体取决于它们的配置。
  • Special extensions are needed on the server 服务器上需要特殊扩展

(Long-) polling (长)轮询

upside 上边

  • Best compatibility (works even in older browsers, no issues with proxies or firewalls) 最佳兼容性(即使在较旧的浏览器中也可以使用,代理或防火墙没有问题)

downside 缺点

  • Creates lots of overhead because it does create for each message a new http connection 由于确实会为每条消息创建一个新的http连接,因此会产生大量开销
  • You need to store intermediate results somewhere because the poll requests come in different threads to your application than your code generation mechanism. 您需要将中间结果存储在某个地方,因为轮询请求与代码生成机制相比,进入应用程序的线程不同。

can you try with 你可以试试吗

if (xmlhttp.readyState == 4){
  xml = xmlhttp.responseText;
  document.getElementById('q').innerHTML=xml;
}}}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM