我的服務器上有一個執行多項任務(設置文件夾、復制文件等)的 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:<?phpfunction 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 個會話?
1 回答

牛魔王的故事
TA貢獻1830條經驗 獲得超3個贊
你不能這樣設置,AJAX 請求會在服務器端產生一個新的 PHP 進程,它不知道第一個進程。
長時間工作的腳本應該將狀態存儲到數據庫中,從那里可以通過第二個(,第三個,...)請求獨立查詢。
- 1 回答
- 0 關注
- 124 瀏覽
添加回答
舉報
0/150
提交
取消