So I know the issue here is that im trying to send a POST request from example.com
to localhost
. What I'm not sure about is why this isn't working, as from what I can tell, I've added everything to Laravel via the nuclear option.
In Laravel, I've added to my public/index.php
the following:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, PUT, POST, DELETE, OPTIONS'):
I know this isn't the ideal way to do this, however I was having the same issues using Cors middleware too.
My client is a React app using Axios to send a POST request from the browser, and in the network tab I can see the following:
My Axios request looks like so:
await axios({
method: 'POST',
url: 'http://localhost:8000/api/webhooks/platform',
data: notification.data,
headers: {
Origin: 'https://example.com'
}
});
Does anyone know where I'm going wrong? My versions are below, and are strict. I cannot change these:
{
"php": "^7.1.3",
"laravel/framework": "5.8.*",
"laravel/nexmo-notification-channel": "^2.5",
"laravel/tinker": "^1.0",
"nexmo/laravel": "^2.4",
}
I tried this answer but I'm getting Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource.
app/Http/Middleware/Cors.php
<?php
namespace App\Http\Middleware;
use Closure;
class Cors
{
/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @return mixed
*/
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-Credentials', true)
->header('Access-Control-Allow-Headers', 'Authorization,Accept,Origin,DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range');
}
}
Kernel.php
protected $routeMiddleware = [
......
'cors' => \App\Http\Middleware\Cors::class,
];
routes/api.php
Route::post('/webhooks/platform', 'WebhooksController@handlePlatformNotification') -> middleware('cors');