Laravel 5:仅在一个 URL 上路由 CORS 问题
Laravel 5: routing CORS issue on just one URL
我正在尝试向外部 laravel 站点发出 2 个 ajax 请求。其中一项请求完美无缺 ("list")。另一个 ("savedevice") 给我以下错误:
从来源 'null' 访问“http://localhost/somesite/devicecreate”处的 XMLHttpRequest 已被 CORS 策略阻止:没有 'Access-Control-Allow-Origin' header 存在于请求的资源。
来源为空,因为请求来自本地html。
我已经创建了适用于第一条路线但不适用于第二条路线的 CORS 中间件解决方案。
2条路由在web.php中存储如下:
Route::post('/devicecreate','FrontEndController@savedevice')->middleware('cors');
Route::post('/list', 'FrontEndController@list')->middleware('cors');
这是我在 javascript
中的 ajax 请求函数
var ajaxRequest = function ( url, data, callback ) {
var xhr = new XMLHttpRequest();
xhr.onerror = function(e) {
console.log("Ajax request error");
};
xhr.addEventListener("load",function () {
xhr.responseJSON = JSON.parse( xhr.responseText );
callback( xhr.responseJSON);
});
xhr.open("POST", url );
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
xhr.send(data);
};
目前(出于测试目的)两种路由方法做同样的事情。但只有“/list”有效。
如果我尝试 php artisan route:list 我可以同时看到 "devicecreate" 和 "list"相同的方法、正确的操作和相同的中间件
我的 CORS 中间件如下所示:
<?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)
{
if ($request->getMethod() == "OPTIONS") {
return response(['OK'], 200)
->withHeaders([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET,POST',
'Access-Control-Allow-Headers' => 'Authorization,Content-Type,X-Requested-With,XMLHttpRequest',
]);
}
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET,POST')
->header('Access-Control-Allow-Headers','Authorization,Content-Type,X-Requested-With,XMLHttpRequest');
}
}
我也试过 运行 php artisan route:cache.
我试过重命名路线,但没有任何区别。
有人能帮忙吗?
24 小时后,我为遇到以下任一情况的任何人提供了解决方案:
- 来自外部源的 CORS / 跨域错误发布 ajax(否则预检)
- 419 未知状态
- 从外部站点发帖时出现围绕 CSRF 令牌的问题。
1) 创建一个 CORS 中间件 (https://laravel.com/docs/5.8/middleware)。您可以使用上面显示的我的中间件代码。
2) 确保将中间件添加到受保护的 $routeMiddleware 下的 Http/Kernal.php 文件中,例如:
'cors' => \App\Http\Middleware\Cors::class,
3) 仅将中间件附加到需要它的路由。就我而言,这是:
Route::post('/setdevice','FrontEndController@savedevice')->middleware('cors');
Route::post('/list', 'FrontEndController@list')->middleware('cors');
4) 记得从 CSRF Token 验证中排除你的外部 Ajax 请求! 我的问题是我忘了添加第二条路线!!所以在我的例子中,我将这些添加到 Http/Middleware/VerifyCsrfToken.php:
中受保护的 $except 参数
protected $except = [
'list',
'savedevice',
];
我正在尝试向外部 laravel 站点发出 2 个 ajax 请求。其中一项请求完美无缺 ("list")。另一个 ("savedevice") 给我以下错误:
从来源 'null' 访问“http://localhost/somesite/devicecreate”处的 XMLHttpRequest 已被 CORS 策略阻止:没有 'Access-Control-Allow-Origin' header 存在于请求的资源。
来源为空,因为请求来自本地html。
我已经创建了适用于第一条路线但不适用于第二条路线的 CORS 中间件解决方案。
2条路由在web.php中存储如下:
Route::post('/devicecreate','FrontEndController@savedevice')->middleware('cors');
Route::post('/list', 'FrontEndController@list')->middleware('cors');
这是我在 javascript
中的 ajax 请求函数var ajaxRequest = function ( url, data, callback ) {
var xhr = new XMLHttpRequest();
xhr.onerror = function(e) {
console.log("Ajax request error");
};
xhr.addEventListener("load",function () {
xhr.responseJSON = JSON.parse( xhr.responseText );
callback( xhr.responseJSON);
});
xhr.open("POST", url );
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
xhr.send(data);
};
目前(出于测试目的)两种路由方法做同样的事情。但只有“/list”有效。
如果我尝试 php artisan route:list 我可以同时看到 "devicecreate" 和 "list"相同的方法、正确的操作和相同的中间件
我的 CORS 中间件如下所示:
<?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)
{
if ($request->getMethod() == "OPTIONS") {
return response(['OK'], 200)
->withHeaders([
'Access-Control-Allow-Origin' => '*',
'Access-Control-Allow-Methods' => 'GET,POST',
'Access-Control-Allow-Headers' => 'Authorization,Content-Type,X-Requested-With,XMLHttpRequest',
]);
}
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET,POST')
->header('Access-Control-Allow-Headers','Authorization,Content-Type,X-Requested-With,XMLHttpRequest');
}
}
我也试过 运行 php artisan route:cache.
我试过重命名路线,但没有任何区别。
有人能帮忙吗?
24 小时后,我为遇到以下任一情况的任何人提供了解决方案:
- 来自外部源的 CORS / 跨域错误发布 ajax(否则预检)
- 419 未知状态
- 从外部站点发帖时出现围绕 CSRF 令牌的问题。
1) 创建一个 CORS 中间件 (https://laravel.com/docs/5.8/middleware)。您可以使用上面显示的我的中间件代码。
2) 确保将中间件添加到受保护的 $routeMiddleware 下的 Http/Kernal.php 文件中,例如:
'cors' => \App\Http\Middleware\Cors::class,
3) 仅将中间件附加到需要它的路由。就我而言,这是:
Route::post('/setdevice','FrontEndController@savedevice')->middleware('cors');
Route::post('/list', 'FrontEndController@list')->middleware('cors');
4) 记得从 CSRF Token 验证中排除你的外部 Ajax 请求! 我的问题是我忘了添加第二条路线!!所以在我的例子中,我将这些添加到 Http/Middleware/VerifyCsrfToken.php:
中受保护的 $except 参数 protected $except = [
'list',
'savedevice',
];