webman接口 get正常,post會(huì)出現(xiàn)跨域問題,已經(jīng)按照官網(wǎng)手冊里的跨域中間件正確配置了。
use app\middleware\AccessControl;
return [
'' => [
AccessControl::class,
],
];
我的控制器是
app/controller/AuthController
route是
Route::post('/auth/login', [AuthController::class, 'login']);
頁面報(bào)錯(cuò)信息:
Access to XMLHttpRequest at 'http://192.168.1.3:8787/api/auth/login' from origin 'http://localhost:5173' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
前端vite配置:
server: {
proxy: {
"/api": {
target: "http://192.168.1.3:8787",
changeOrigin: true
}
}
}
login post方法
General
Request URL: http://192.168.1.3:8787/api/auth/login
Referrer Policy: strict-origin-when-cross-origin
Request Headers
Provisional headers are shown
Learn more
Accept: application/json, text/plain, */*
Content-Type: application/json
Referer: http://localhost:5173/
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
login options
General
Request URL: http://192.168.1.3:8787/api/auth/login
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: 192.168.1.3:8787
Referrer Policy: strict-origin-when-cross-origin
Request Headers
Connection: keep-alive
Content-Length: 34
Content-Type: application/json
Server: workerman
workerman通過搜索跨域相關(guān)問題,有遇到和我一樣的情況,也沒有解決方案。百度也搜索了vite跨域相關(guān)配置,確定已經(jīng)按照文檔正確配置了跨域。
看下服務(wù)端是否有錯(cuò)誤日志,可能500了。
瀏覽器里network里看http狀態(tài)碼是否是200、返回的http頭是否帶上了跨域header。
大佬,network里post請求沒有返回任何狀態(tài)碼,options請求返回200狀態(tài)碼,我把請求的狀態(tài)碼重新編輯放在上面問題里了,不知道是什么原因
解決了,我的錯(cuò)。。。。找了老半天,沒好好看文檔
提示
跨域可能會(huì)產(chǎn)生OPTIONS請求,我們不想OPTIONS請求進(jìn)入到控制器,所以我們?yōu)镺PTIONS請求直接返回了一個(gè)空的響應(yīng)(response(''))實(shí)現(xiàn)請求攔截。
如果你的接口需要設(shè)置路由,請使用Route::any(..) 或者 Route::add(['POST', 'OPTIONS'], ..)設(shè)置。
大佬,webman兩個(gè)接口,一個(gè)設(shè)置session,另一個(gè)接口拿不到session的值,是什么原因額?在webman-admin接口中確是可以的
解決了,有遇到同樣問題的同學(xué),好好看文檔吧。。。。。
提示
跨域可能會(huì)產(chǎn)生OPTIONS請求,我們不想OPTIONS請求進(jìn)入到控制器,所以我們?yōu)镺PTIONS請求直接返回了一個(gè)空的響應(yīng)(response(''))實(shí)現(xiàn)請求攔截。
如果你的接口需要設(shè)置路由,請使用Route::any(..) 或者 Route::add(['POST', 'OPTIONS'], ..)設(shè)置。