截至昨天,下面的代码有效。然而今天,我必须在 laravel 中运行 php artisan config:cache 命令,因为我添加了一个包,现在我漂亮的 ionic 应用程序不想运行连接到任何东西,因为我不断收到此 403 错误。
在我安装“rap2hpoutre/laravel-log-viewer”:“^0.19.1”并缓存后开始出现错误,但我认为这与它没有任何关系。我很确定我的缓存在此之前是最新的。
jwt 产生相同的错误。
以前,该应用程序无需 cors 插件即可运行。
它在本地和我的服务器上向我提供了此错误(因为我也必须在那里缓存)。
这个错误与我之前调试时遇到的错误不同。
当我拉路线时http://xxx/api/home http://xxx/api/home通常在 chrome 中 - 它返回正常...在邮递员中相同
感谢您的帮助!
错误
OPTIONS http://xxx/api/home http://xxx/api/home403(禁止)
加载失败http://xxx/api/home http://xxx/api/home:预检响应没有 HTTP 正常状态。
IONIC
basicGet_no_token(rl){
console.log('basicGet - ' + this.base_url + rl);
return new Promise((resolve, reject) => {
this.http.get(this.base_url + rl,
{headers: new HttpHeaders({
'Content': 'application/json',
'Accept': 'application/json',
})})
.subscribe(res => {console.log(res);resolve(res);}, (err) => {this.handleError(err);console.log(err);reject(err);});
});
}
LARAVEL
Route::group(['middleware' => ['addHeaders']], function () {
Route::get('home', 'api\WelcomeController@home');
});
class addHeaders
{
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, PUT, DELETE',
'Access-Control-Allow-Credentials' => true,
'Access-Control-Allow-Headers' => 'Origin, Content-Type, X-Auth-Token, authorization, X-Requested-With'
]);
}
return $next($request)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE')
->header('Access-Control-Allow-Credentials', true)
->header('Access-Control-Allow-Headers', 'Origin, Content-Type, X-Auth-Token, authorization, X-Requested-With');
}
}
class WelcomeController extends Controller
{
public function home()
{
$r['message']="Welcome!";
$r['allow']=true;
$p=compact('r');
return response()->json($p, 200);
}
}
class Kernel extends HttpKernel
{
protected $middlewareGroups = [
'api' => [
'throttle:60,1',
'bindings',
],
];
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'jwt' => \App\Http\Middleware\JWT::class,
'addHeaders' => \App\Http\Middleware\addHeaders::class,
'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class,
'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
];
}