Answer the question
In order to leave comments, you need to log in
Laravel: POST and GET request not working, requires Access-Control-Allow-Origin, how to fix?
In general, I rummaged through everything that is possible, set everything up, did it, and still requires a title, I don’t understand what’s happening anymore, I’m attaching screenshots, where everything is written, help, what else is missing, why doesn’t it want to work?
File controller:
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class restController extends Controller
{
public function index()
{
echo "gg";
}
}
namespace App\Http\Middleware;
class Cors
{
public function handle($request, \Closure $next)
{
$response = $next( $request );
$response->header('Access-Control-Allow-Origin', 'localhost');
$response->header('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, OPTIONS');
$response->header('Access-Control-Allow-Headers', 'Origin, Content-Type');
return $response;
}
}
window.onload = init;
function init() {
var button = document.querySelector('#btn');
button.addEventListener('click', async function (e) {
e.preventDefault();
//console.log(document.querySelector('meta[name="csrf-token"]').content);
let url = 'http://127.0.0.1:8000/photoService/registration/create';
let user = {
name: 'John',
surname: 'Smith'
};
let promise = await fetch(url, {
method: 'GET',
headers: {
'Content-Type': 'application/json;charset=utf-8',
'Accept': 'application/json',
},
//body: user
});
let result = await promise.json();
console.log(result);
});
}
namespace App\Http;
use Illuminate\Foundation\Http\Kernel as HttpKernel;
class Kernel extends HttpKernel
{
protected $middleware = [
// ...
\App\Http\Middleware\Cors::class,
];
}
<?php
Route::get('photoService/registration/create', '[email protected]')->middleware('cors');
Answer the question
In order to leave comments, you need to log in
Well, look at what requests are sent ... I suspect that OPTIONS is sent, in response to which no header comes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question