thinkphp8 上传文件跨域
路由改为:Route::post('file_upload', 'account.ZgrOss/oss_upload');接口写好了,用postman 访问没有问题,前端调用时竟然直接 跨域,我写了一个 html 上传页面,也是跨域。还有一处不要忽略,也是我死活都报跨域的原因,之前没有添加 下面红框里的内容,加上就好了。然后下面是正确的 代码和 配置(我用的是宝塔)试了网上的好多方法,也问过dee
路由器代码
Route::post('file_upload', 'account.ZgrOss/oss_upload')->allowCrossDomain();
控制器代码
public function oss_upload(){ try{ if(isset($_FILES['file'])){ dd($_FILES['file']);
接口写好了,用postman 访问没有问题,前端调用时竟然直接 跨域,我写了一个 html 上传页面,也是跨域
试了网上的好多方法,也问过deepseek
然后下面是正确的 代码和 配置(我用的是宝塔)
路由改为:Route::post('file_upload', 'account.ZgrOss/oss_upload');
nginx 配置
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
还有一处不要忽略,也是我死活都报跨域的原因,之前没有添加 下面红框里的内容,加上就好了
location / {
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
add_header 'Access-Control-Max-Age' 1728000;
return 204;
}
if (!-e $request_filename){
rewrite ^(.*)$ /index.php?s=$1 last; break;
}
}
更多推荐
所有评论(0)