Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.9k views
in Technique[技术] by (71.8m points)

js EventSource

客户端:

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

服务器端:

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

$token = $_GET['token'];
echo "token: The token is: {$token}

";
flush();
?>

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

又自立自强了!!!

前台用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}---

";
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却不行.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...