Allow No 'Access-Control-Allow-Origin' header is present
2017-02-21 09:23
239 查看
后台使用laravel框架,前端请求数据报错 Allow No 'Access-Control-Allow-Origin' header is present
解决办法:
一、在App\Http\Middleware目录下新建Cors.php
二、在项目配置文件config\star.php 文件中新增跨域的配置信息
三、讲Cros.php中间件加入到App\Http\Kernel.php中
以上是正常请求,为了处理异常请求的返回,在App\Exceptions\Hander.php中增加header信息
解决办法:
一、在App\Http\Middleware目录下新建Cors.php
<?php namespace App\Http\Middleware; use Closure; class Cors { /** * Handle an incoming request. * * @param \Illuminate\Http\Request $request * @param \Closure $next * @return mixed */ public function handle($request, Closure $next) { $response = $next($request); $headers = config('star.cors', []); $response->headers->add($headers); return $response; } }
二、在项目配置文件config\star.php 文件中新增跨域的配置信息
/* |-------------------------------------------------------------------------- | CORS跨域配置 |-------------------------------------------------------------------------- | */ 'cors' => [ 'Access-Control-Allow-Origin' => '*', 'Access-Control-Allow-Credentials' => 'true', 'Access-Control-Allow-Headers' => 'Origin, X-Requested-With, Content-Type, Accept, Cache-Control, Authorization', 'Access-Control-Allow-Methods' => 'HEAD, GET, POST, PUT, PATCH, DELETE, OPTIONS, TRACE', ],
三、讲Cros.php中间件加入到App\Http\Kernel.php中
protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\Cors::class, ];
以上是正常请求,为了处理异常请求的返回,在App\Exceptions\Hander.php中增加header信息
$headers = config('star.cors', []); $response->headers->add($headers);
相关文章推荐
- JS跨域:No 'Access-Control-Allow-Origin' header is present on the requested resource
- jquery ajax No 'Access-Control-Allow-Origin' header is present on the requested resource
- 报No 'Access-Control-Allow-Origin' header is present on the requested resource
- 跨域 No 'Access-Control-Allow-Origin' header is present on the requested resource
- jquery跨域访问,no 'access-control-allow-origin' header is present on the requested resource
- 解决Vue请求 ‘No 'Access-Control-Allow-Origin' header is present on the requested resource’错误
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is there
- 找到的OpenLayers跨域访问WFS服务出现No 'Access-Control-Allow-Origin' header is present...错误的一种解决办法
- ajax跨域请求:No 'Access-Control-Allow-Origin' header is present on the requested resource
- 本地Ajax跨域访问 No 'Access-Control-Allow-Origin' header is present on the requested resource.
- No 'Access-Control-Allow-Origin' header is present on the requested resource
- 跨域问题:No 'Access-Control-Allow-Origin' header is present on the requested resource.
- 解决 js ajax跨域访问报“No 'Access-Control-Allow-Origin' header is present on the requested resource.”错误
- No 'Access-Control-Allow-Origin' header is present on the requested resource
- No 'Access-Control-Allow-Origin' header is present on the requested resource.
- Vue报错笔记(2):No 'Access-Control-Allow-Origin' header is present on the requested resource.
- No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '
- 调用其他服务器的api时,暂时处理跨域问题的方法——No 'Access-Control-Allow-Origin' header is present on the requested resour
- 解决 ajax跨域 No 'Access-Control-Allow-Origin' header is present on the requested resource.
- No 'Access-Control-Allow-Origin' header is present on the requested resource.