Error in CORS Ionic 5.2 - PHP API

Asked

Viewed 107 times

0

I’m trying to connect my application with a PHP API.

I have the following Ionic code in which the authentication is done by Token:

login(data:IUsuario) {
    let tokenBasic = btoa(data.usuario + ":" + data.senha);
    let headerBasic = {
      "headers": { 
        'Authorization': "Basic " + tokenBasic,
        'Content-Type': 'application/json',
        'cache-control': 'no-cache'
      }
    };
    return this.http.get<any>(this.url + '/AppLogin', headerBasic);
  } 

In my PHP API the code already contains headers for CORS:

<?php
header('Access-Control-Allow-Headers: "Origin, X-Requested-With, Content-Type, Accept"');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET, POST, PATCH, PUT, DELETE, OPTIONS');
header('Content-Type: application/json; charset=utf8');

echo json_encode((object) array( 'Auth'=> 'true', 'Data'=>'04/08/2019', 'Usuario'=> 'teste'));
?>

But when making any query by the browser, I get the following error:

inserir a descrição da imagem aqui

On the network:

inserir a descrição da imagem aqui

  • Dear Gustavo, it seems that using a FW of routes, because the url is not final . php, then probably your headers have not even been sent, with the details of the question nor have to help you, have to explain right what you did to get help you

  • Guilherme, in this case I’m using the Codeigniter framework in PHP, but the PHP code I mentioned, I just wanted to simplify accessing only it (test.php) without routes, so as soon as the headers work in this file I apply throughout the framework.

  • By the answer the app did not work no, do the test by opening the URL directly via browser, probably something failed in your back-end

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.