Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ionic 3 + laravel. issue with http request on server

I'm developing a hybrid app with Ionic 3 and Laravel as Back-end. I had to host the backend on an online server (000webhost) to do some in-app testing, that's when it started my problems. Before hosting, using my computer as a server (localhost), I was able to make any type of request through Ionic providers. Using webhost hosting, I can only make GET requests, any other type of request give me the MethodNotAllowedHttpException error. Trying to make the same type of request using the postman it is processed successfully.

My configuration file cors.php is as follow:

<?php

return [

    /*
    |--------------------------------------------------------------------------
    | Laravel CORS
    |--------------------------------------------------------------------------
    |
    | allowedOrigins, allowedHeaders and allowedMethods can be set to array('*')
    | to accept any value.
    |
    */

    'supportsCredentials' => false,
    'allowedOrigins' => ['*'],
    'allowedOriginsPatterns' => [],
    'allowedHeaders' => ['*'],
    'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE'],
    'exposedHeaders' => [],
    'maxAge' => 0,

];

he route I'm trying to access:

Route::post ('ajudado/', 'AjudadoController@set_ajudado');

the provider method in Ionic:

set_ajudado(dados):Promise<any>
  {
    return this.http.post(this.url, dados).toPromise().then(function(data){
      return  data; 
   });
  }
like image 582
Guilherme M Avatar asked Sep 12 '26 14:09

Guilherme M


1 Answers

You will need to allow the OPTIONS method in your CORS rules.

'supportsCredentials' => false,
'allowedOrigins' => ['*'],
'allowedOriginsPatterns' => [],
'allowedHeaders' => ['*'],
'allowedMethods' => ['GET', 'POST', 'PUT', 'DELETE', 'OPTIONS'],
'exposedHeaders' => [],
'maxAge' => 0,
like image 188
Joe Avatar answered Sep 15 '26 06:09

Joe