This error occurs because of the CORS policy. To fix this, you need to add the 'Access-Control-Allow-Origin' header to the response from the Laravel API. You can do this by adding the following code to your Laravel controller:
public function store(Request $request)
{
// your existing code
return response()->json(['Data inserted successfully' => true, 'data' => $project], 200)
->header('Access-Control-Allow-Origin', '*')
->header('Access-Control-Allow-Methods', 'POST');
}
This will allow any origin to access the API and the POST method. You can also replace the '*' with the specific origin that you want to allow.
Additionally, you can use the Laravel CORS package to handle CORS in a more organized way. You can install it using composer:
composer require fruitcake/laravel-cors
Then, add the following code to your Laravel config/cors.php file:
return [
'paths' => ['api/*'],
'allowed_methods' => ['*'],
'allowed_origins' => ['*'],
'allowed_origins_patterns' => [],
'allowed_headers' => ['*'],
'exposed_headers' => [],
'max_age' => 0,
'supports_credentials' => false,
];
This will allow any origin to access the API. You can also customize the allowed paths, methods, and headers according to your needs.