文档说明: 只记录关键地方; 文档创建时间:2023-08-06;
目标:使用 JSONP 方式解决跨域
备注: 常用到的功能记录一下,方便复制
<?php
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Coroutine\Http\Server;
use function Swoole\Coroutine\run;
run(function () {
$server = new Server('0.0.0.0', 9502, false);
$server->handle('/', function (Request $request, Response $response) {
$response->header('Content-Type', 'application/javascript; charset=utf-8');
$callback = !empty($request->get['callback']) ? $request->get['callback'] : 'callback';
$result = [];
try {
$result = [
'code' => 200,
"msg" => 'success',
"data" => []
];
} catch (\RuntimeException $e) {
echo $e->getMessage();
$result = ["code" => 500, 'msg' => 'system error' . $e->getMessage()];
}
$response->end($callback . "(" . json_encode($result, JSON_UNESCAPED_UNICODE) . ')');
});
$server->start();
});
标签:跨域,swoole,request,callback,result,JSONP,response
From: https://www.cnblogs.com/jingjingxyk/p/17609207.html