我的服务器上有一个执行多项任务(设置文件夹、复制文件等)的 php 脚本,我用 ajax 调用该脚本。
IE
注册.js:
const evtSource = new EventSource("/createuser.php", { withCredentials: true } );
evtSource.onmessage = function(event) {
console.log(event.data);
}
$.ajax({
type: 'POST',
url: '/createuser.php',
dataType: 'json',
data: {
'username': "test user",
'usertype' : "author"
},
success: function(data){
console.log("user created");
}
});
现在在 createuser.php 上,我尝试向我的网页发送一条消息:
创建用户.php:
<?php
function SendProgressMessage($op){
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
switch($op){
case "userCreated":
echo "User Created";
break;
case "foldersCreated":
echo "Folder structure created";
break;
case "TemplatesCopied":
echo "Templates Copied";
break;
}
flush();
}
?>
我可以将 evtSource 设置为相同的脚本和 ajax 调用,还是为脚本创建 2 个会话?
牛魔王的故事