IOnic 2 error http request

0

I need to make a request to a webserver on ionic 2 and I have the following code:

constructor(private navController: NavController, public http: Http) {
      var url = 'http://localhost/APIPortManager/teste.php';
      var response = this.http.get(url)
      response.toPromise().then(response => {
         
          let dados = response.json()
          alert (dados)
      }).catch(err => {
      //let erro = err.json();
          alert ("erro")
      });
        //return response;
      //alert(response.toPromise()); // return object object
  }

But only the error returns, how do I display the text? Thank you Ps: I know the correct one is not in the controller, I just need to display the code that pg has, I do not even need json if the pg display hi I want to give alert on hi only this. Update, console error:

  

XMLHttpRequest can not load link . At the   'Access-Control-Allow-Origin' header is present on the requested   resource. Origin ' link ' is therefore not allowed   access.

    
asked by anonymous 19.11.2016 / 12:18

1 answer

0

In test.php, you need to add some HEADERS in the response (also known as CORS).

Here's an example: ionic2 - photo upload via server

if (isset($_SERVER['HTTP_ORIGIN'])) {
    header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
    header('Access-Control-Allow-Credentials: true');
    header('Access-Control-Max-Age: 86400');    // cache for 1 day
}

// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
    header("Access-Control-Allow-Methods: GET, POST, OPTIONS");

    if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
    header("Access-Control-Allow-Headers:        {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");

    exit(0);
}
    
27.01.2017 / 16:01