javascript - js EventSource
PHP中文网
PHP中文网 2017-04-11 12:40:01
[JavaScript讨论组]

客户端:

var source = new EventSource("http://controller.shujudaping.com:8084?token=1");

服务器端:

怎么才能在服务器端拿到token的数据?

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(1)
巴扎黑

又自立自强了!!!

前台用event.data获取服务器端的数据

document.ready(function() {
  document.domain = 'shujudaping.com';
  var source = new EventSource("http://controller.shujudaping.com:8084/?token=2");
  source.onmessage = function(event) {
    document.getElementById("result").innerHTML += event.data + "<br>";
  };
})

服务器端:

<?php
header("Access-Control-Allow-Origin: *");
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');

$token = $_GET['token'];
echo "data: The token is: ---{$token}---\n\n";
flush();
?>

我之前在前台获取服务器的数据是通过event.data来获取,但是在服务器端处理请求的时候写的是echo "token: The token is: ---{$token}---nn";将其改成echo "data: The token is: ---{$token}---nn";就可以了.但是当我不改服务器端的echo "token: The token is: ---{$token}---nn";而是在前台将event.data改成event.token却不行.

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号