Htt.post ionic 2 + php

hi,

I’m trying to do a http post using the http module in Ionic 2.
The POST request hits the server OK but no post data is received. Any idea what I might be doing wrong?

This is my code:

autentication(email, password) {

let headers = new Headers({
		'Content-Type': 'application/x-www-form-urlencoded'
	});

let options = new RequestOptions({
		headers: headers
	});

let link = "my server";

let body = 'email=' + email + '&password=' + password;

return this.http.post(link, body, options)
    .map(res => {
      return this.myData = res.json() ? JSON.parse(this.myData) : {}
    })
    .subscribe(
      data => this.myData = data ? JSON.parse(this.myData) : {},
      err  => console.log("Error: " + err)
    );
}

my error:

SyntaxError: Unexpected token < in JSON at position 0

I’m going to guess “running on iOS and using http (as opposed to https)”.

No, I’m in the browser running, using php for communication with mysql

Well, your server appears to be returning HTML, not JSON.

this is my server code:

 <?php
           header("Access-Control-Allow-Origin: *");
           header("Access-Control-Allow-Headers: ACCEPT, CONTENT-TYPE, X-CSRF-TOKEN");
           header("Access-Control-Allow-Methods: GET, POST, OPTIONS, DELETE");

    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);
}

$dns = "mysql:host=;dbname=";
$user = "";
$pass = "";
try {
    $con = new PDO($dns, $user, $pass);
        if(!$con){
	        echo "Unable to connect to Database!";
}

$postdata = file_get_contents("php://input");
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
	$email = (isset($_POST['email'])) ? $_POST['email'] : '';
	$senha = (isset($_POST['password'])) ? $_POST['password'] : '';
	
	if ($email != "" && $password != "") {
	    $sel = $con->mysql_query("SELECT * FROM user WHERE email='$email' AND password='$password'");
	        if($sel->rowCount()>1) {
		   		return true;
		}
		else {
			header('HTTP/1.1 401 Unauthorized', true, 401);
		}
	    	echo "Not called properly with username parameter!";
	}
} 

} catch (Exception $e) {
	echo "Erro: ". $e->getMessage();
};
?>

Your browser should have the ability to show you what is going over the wire. In Chrome’s Developer Tools, it’s the Network tab. Firebug has something similar.

I`m back : Failed to load resource: net::ERR_FAILED apiLogin.php