How to handle a put request to SLIM?

Chrome swears here such words
Failed to load http://127.0.0.1/api/v1.0/item/1: Response to a preflight request doesn't pass the access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.


in the case of GET request helped setting the title

return $response
 ->withHeader('Access-Control-Allow-Origin', '*')
 ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
 ->withJSON(...


But PUT request does not work =(

I tried to install the header doesn't help

If you believe chromium is desired, the query returns
Allowed methods: GET, PUT, POST, DELETE, PATCH

Well, the question is: How to properly handle PUT requests for slim

Code API
<?php
 use \Psr\Http\Message\ServerRequestInterface as Request;
 use \Psr\Http\Message\ResponseInterface as Response;

 require 'vendor/autoload.php';

 $app = new \Slim\App;

 $app->get("/items/{first}/{amount}", function($request, $response, $args) {
 return $response
 ->withHeader('Access-Control-Allow-Origin', '*')
 ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
->withJSON(
[
 "itemsCount" => 1,
 "items" => [
[
 "id" => 1,
 "name" => "Shapka",
 "description" => "Description"
],
[
 "id" => 2,
 "name" => "Zontik",
 "description" => "Opisanie zontika"
]
]
],
200,
JSON_UNESCAPED_UNICODE
);
});

 $app->put("/item/{id}", function($request, $response, $args) {
 return $response
 ->withHeader('Access-Control-Allow-Origin', '*')
 ->withHeader('Access-Control-Allow-Headers', 'X-Requested-With, Content-Type, Accept, Origin, Authorization')
 ->withHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS')
->write("done");
});

 // Catch-all route to serve a 404 Not Found page if none of the routes match
 // NOTE: make sure this route is defined last
 $app->map(['GET', 'POST', 'PUT', 'DELETE', 'PATCH'], '/{routes:.+}', function($req, $res) {
 $handler = $this->notFoundHandler; // handle Slim using the default page not found handler
 return $handler($req, $res);
 }); 

$app->run();
?>

Request code
updateItem(item) {
 console.log("update item with id: ", item)

 let xhr = new XMLHttpRequest()
 xhr.open("PUT", `http://127.0.0.1/api/v1.0/item/${item.id}`)
 xhr.setRequestHeader("Content-Type", "application/json;charset=UTF-8")
xhr.send("{\"name\":\"azaza\"}")

this.updateItemsFromRestApi()
 }
March 11th 20 at 19:26
1 answer
March 11th 20 at 19:28
And the face on 127.0.01 is?
If the face is the client side, then Yes, it is, but in the long term could be anywhere. - terrill_OHara commented on March 11th 20 at 19:31
@terrill_OHara, in General, this error occurs when the sent ajax requests to other than the current page domain.
and Access-Control-Allow-Headers needs to give the server when loading the muzzle, this is the server to the client may allow such appeals, but not the client itself. - fiona89 commented on March 11th 20 at 19:34

Find more questions by tags Slim FrameworkRESTful API