afgprogrammer/PHP-MVC-REST-API

Unable to Call API with custome header data

adminrai opened this issue · 10 comments

Hi
Please provide me help i am unable to call api from react with custome header data.

const response = fetch('http://localhost/server/logout',{headers: {'Auth': 'foobar'}});

Its through Error:
Access to fetch at 'http://localhost/server/logout' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status.

Try to call your route with Postman or any other API test service, if you still have this problem let me know.

I believe the issue is with the preflight OPTIONS method. I don't believe a service like Postman uses any preflight requests. I'd suggest adding something like this to the index.php file:

if ( 'OPTIONS' === $request->getMethod() ) {
    
    // fake set request url and method
    $router = new Router('/', "GET");
    
    $content = [
        "method" => $request->getMethod()
    ];
    
    $response->sendStatus(200);
    $response->setContent($content);
    
}

Thanks for your supports Its Working fine.
But now i face another problem.
On my localhost when i call api its runing fine.
But i upload this dir to server try to call API it show errro its show error.

Please help me.

@adminrai what error?

Is show 500 error

@adminrai The error doesn't have any content❓

This page isn’t working
relopsonline.com is currently unable to handle this request.
HTTP ERROR 500

When i dubug it in index.php
Before code
**use Router\Router;

// create object of request and response class
$request = new Http\Request();
$response = new Http\Response();**

is working but afrer this code its through error 500

@adminrai Your application hosted on this website 'relopsonline.com' or it's your endpoint?

Yes hosted on relopsonline.com and end point is