[SOLVED] 405 Method Not Allowed using Angular 4's HttpClient


#1

I’m using slim3 as my web API and ionic framework for my mobile app. I’m trying to implement log-in functionality and I’m sending my post request using angular’s new HttpClient as follows:

return this.http.post(http://xyz.com/api/public/v1/login, credentials).subscribe( data => {
    alert(JSON.stringify(data));
}, err => {
    alert(JSON.stringify(err));
});

My slim routes are set up as follows:

$app->options(’/{routes:.+}’, function (Request $request, Response $response, array $args) {
    return $response;
});
$app->add(function (Request $req, Response $res, $next) {
    $response = $next($req, $res);
    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, OPTIONS');
});
//Login
$app->post('/v1/login', function(Request $req, Response $res, array $args){
    $data = $req->getParsedBody();
    $email = $data['login'];
    $password = md5($data['password']);
    //Query db for user
    return $this->response->withJson(true);
});

But when I run the app, I get a 405 Method Not Allowed error with the message: Method not allowed. Must be one of : OPTIONS.

I realize this might be a SlimPHP forum question but I also thought it would be appropriate here since accessing the same URL with Postman and providing the required form body gives me the expected output but the app gives the Slim PHP 405 error. I therefore thought this could be Angular-related. I could be wrong though.

Could anyone please point out what I might be doing wrong?


#2

Turns out it was a phone related issue. Tried another phone and it worked like a a charm.