Lumen API CORS Ajax 405 Method Not Allowed

后端 未结 3 2009
忘了有多久
忘了有多久 2021-01-16 13:42

I have an api on Laravel Lumen, we test via Postman and Ruby Rest Client and all go very well, but we create a simple Auth Login that response a web token, all works fine bu

3条回答
  •  误落风尘
    2021-01-16 14:37

    Finally I found a clean way for it. Just add the following lines to the .htaccess (If you don't know, .htaccess has been located at public/.htaccess):

    
        Header add Access-Control-Allow-Origin "*"
        Header set Access-Control-Allow-Headers "*"
        Header set Access-Control-Allow-Methods "PUT, GET, POST, DELETE, OPTIONS"
    
    

    Then, define a route in your Laravel/Lumen application like this:

    Route::options('/{any:.*}', function() { return response(['status' => 'success']); });};
    

    Of course you need mod-rewrite and mod-headers. You can enable them (if not already enabled) using the following commands (Ubuntu):

    a2enmod rewrite
    a2enmod headers
    systemctl reload apache2
    

提交回复
热议问题