Cors error with Larable 5.1

Asked

Viewed 251 times

0

inserir a descrição da imagem aquiPersonal I am having problems with the CORS in my application Aravel 5.1, when I make a request via RESTLET it returns without problems. However, if the same request is made via AJAX it generates the Cors ERROR. What do I do? I’ve come across this before and managed to tidy up, but in this case I’m not getting.

  • This AJAX request of yours is leaving which domain?

  • are two different domains, both are local.

  • Try adding the domain to CORS in the domain name https://medium.com/@petehouston/allow-Cors-in-Laravel-2b574c51d0c1 is in English I hope to help.

  • puts already made it, didn’t work. thanks so much for the help.

  • Add the code to your question so we can help you.

  • I uploaded the application to a domain. and placed the image.

  • You have thus added ->header(?Access-Control-Allow-Origin', ?*')?

  • yes. no Cors.php public Function Handle($request, Closure $next) { header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS'); header('Access-Control-Allow-Headers: Content-Type, Accept, Authorization, X-Requested-With, Application'); Return $next($request); }

Show 3 more comments

2 answers

0

Fabricio,

adds the class CORS app/Http/Middleware/Cors.php

class Cors
{
    public function handle($request, Closure $next)
    {
        return $next($request)
          ->header('Access-Control-Allow-Origin', '*')
          ->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS')
          ->header('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, X-Token-Auth, Authorization');
    }
}

and tries to register it in the Kernel app/Http/kernel.php

'cors' => \App\Http\Middleware\Cors::class

that’s how he implemented his CORS?

  • face is exactly like this. But it doesn’t work.

  • He even made a php artisan config:clear ?

  • arrived, the funny and that via RESTLET or POSTMAIN he returns

  • header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE'); header('Access-Control-Allow-Headers: Content-Type, X-Auth-Token, Origin, Authorization'); Route:group:(['/' => 'v1', 'middleware' => 'Cors'], Function () { Route::post('/', 'controller_api@request'); }); Insert into the routes, besides this pulling the middleware

  • thank you so much for helping Sylviot

  • Mark as answer if I helped, dispose!

Show 1 more comment

0

SOLUTION The insertion of the route was made this way below . Route::post('/', 'controller_api@request')->name('/')->middleware('cors');

**IMPORTANT DETAIL: as soon as the inclusion of the 'cors' => \App\Http\Middleware\CorsMiddleware::class, in Kernal.php, do not add anything else related to Access-Control-Allow-Origin. For example :

header('Access-Control-Allow-Origin:  *');
header('Access-Control-Allow-Methods:  POST, GET, OPTIONS, PUT, DELETE');
header('Access-Control-Allow-Headers:  Content-Type, X-Auth-Token, Origin, Authorization');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.