2
votes

I tried to use laravel-cors (https://github.com/barryvdh/laravel-cors) with Laravel 5.2 to update data, but I always get the same error:

XMLHttpRequest cannot load http://dev.pesanlab.com/api/v1/order/cart/add. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 500.

GET request works fine, but POST return error.

return [
    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['GET', 'POST', 'PUT',  'DELETE'],
    'exposedHeaders' => ['*'],
    'maxAge' => 0,
    'hosts' => [],
];

This is my code on Route.php

Route::group(['prefix' => 'api/v1'], function () {
    Route::group(['middleware' => ['web', 'cors']], function () {
        Route::post('order/cart/add','OrderController@cart_add');
    });
});

Could you help me?

1
Can you share some code with us?Franz Wimmer
Remove the web middleware and try again and let's see if that works or not.The Alpha
still error with same messageOhaiyou
Add this header("Access-Control-Allow-Origin: *"); in your public.index.php and check if it works for now.The Alpha

1 Answers

1
votes

Your main problem is not the request but the error that is giving. The error:

XMLHttpRequest cannot load   http://dev.pesanlab.com/api/v1/order/cart/add. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access. The response had HTTP status code 500.

have at the end the status code 500 (Internal server error). Start with the debugging and try to locate the real error:

Enable debug in Laravel:

config/app.php:

'debug' => env('APP_DEBUG', true),

Or use the default php show errors:

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

Or check the laravel.log

Storage/logs/laravel.log

After you manage to do this you can see the original error: