HTTP Request ends in Status:0 for URL: null

Hi guys, hope you can help me out…

I run my app with “ionic serve --lab” and want to try a simple login. With firebase there is no problem but if I want to use mysql + php, the http request always fails. Since I haven’t an own webserver I use one from https://de.000webhost.com/.

So here is the code of the http part:

createEntry(username: string, password: string, email: string) {
    let headers: any = new HttpHeaders({
        'Content-Type': 'application/json',
        'Access-Control-Allow-Origin': '*',
        'Access-Control-Allow-Headers': 'X-API-KEY, Origin, X-Requested-With, Content-Type, Accept, Access-Control-Request-Method',
        'Access-Control-Allow-Methods': 'GET, POST, OPTIONS, PUT, DELETE'
        }),
        options: any = { "key": "create", "username": username, "password": password, "email": email },
        url: any = this.baseURI + "manage-data.json";

    this.http.post(url, JSON.stringify(options), headers).subscribe((data : any) => {
        // If the request was successful notify the user
        this.sendNotification(`Congratulations ${username} was successfully added`);
    }, (error : any) => { this.sendNotification(error); });
  }

The parameters are the data which comes from the submitted form. The url for the http request looks like this: https://wikiloopsapp.000webhostapp.com/manage-data.php

And like the title says it, the request fails and I get an error: Status:0 for URL: null…

1 Like

okay I managed to get the thing working in some case…I can post data to the file and it will be executed but I still get the same error in ionic. So the http request always ends with an error, doesn’t matter if the target file is successfully executed. I don’t know what the problem could be…

What request is the Ionic app actually sending and what HTTP code is your server returning? Look at the browser’s dev tool’s network panel.

Post Request will send to my server adress and it’s 200 Ok. But when I click on response in network panel there is nothing…do I have to code something in my target file? Anyways like I said I get the above error message that the request isn’t successful.

Is it actually doing a POST request?
Please post the exact error messages and best also screenshots of the console and network tab etc.

Error Message : Response with status: 0 for URL: null

Screen of network panel:

Perhaps the error is on the server side.

How do you parse the json body?
What send your server back?

And so on…

Well the server doesn’t send anything back. In the screenshot the tab with answer is empty, just blank. Could be something with the webserver, so the hoster isn’t allowing a response?

I think that you have an error in your php-script and no error handling.
So nothing comes back.

PHP code would be:

<?php
   $hn      = '...';
   $un      = '...';
   $pwd     = '...';
   $db      = '...';
   $cs      = 'utf8';

   $dsn 	= "mysql:host=" . $hn . ";port=3306;dbname=" . $db . ";charset=" . $cs;
   $opt 	= array(
                        PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
                        PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
                        PDO::ATTR_EMULATE_PREPARES   => false,
                       );
try{
    // Create a PDO instance (connect to the database)
    $pdo 	= new PDO($dsn, $un, $pwd, $opt);
}
catch(PDOException $e){
    echo $e->getMessage();
}

   // Retrieve the posted data
   $json    =  file_get_contents('php://input');
   $obj     =  json_decode($json);
   
   $key     =  strip_tags($obj->key);


   // Determine which mode is being requested
   switch($key)
   {

      // Add a new record to the technologies table
      case "create":

         // Sanitise URL supplied values
         $id 		     = filter_var($obj->id, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
         $name	  = filter_var($obj->username, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
		 $password 		     = filter_var($obj->password, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
         $email	  = filter_var($obj->email, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);

         // Attempt to run PDO prepared statement
         try {
            //Check if email already exists
            $sql = "SELECT COUNT(*) FROM user where email = '" . $email . "'";
            $stmt 	= $pdo->prepare($sql);
            $stmt->execute();
            $result = $stmt->fetchColumn();
            
            if($result > 0) {
                echo "Test";
            } else {
                $sql 	= "INSERT INTO user(id, name, password, email) VALUES(:id, :name, :password, :email)";
                $stmt 	= $pdo->prepare($sql);
                $stmt->bindParam(':id', $id, PDO::PARAM_STR);
                $stmt->bindParam(':name', $name, PDO::PARAM_STR);
    			$stmt->bindParam(':password', $password, PDO::PARAM_STR);
    			$stmt->bindParam(':email', $email, PDO::PARAM_STR);
                $stmt->execute();
            }
         }
         // Catch any errors in running the prepared statement
         catch(PDOException $e)
         {
            echo $e->getMessage();
         }

      break;


/*
      // Update an existing record in the technologies table
      case "update":

         // Sanitise URL supplied values
         $name	  = filter_var($obj->username, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
		 $password 		     = filter_var($obj->password, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
         $email	  = filter_var($obj->email, FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_LOW);
         $recordID	     = filter_var($obj->recordID, FILTER_SANITIZE_NUMBER_INT);

         // Attempt to run PDO prepared statement
         try {
            $sql 	= "UPDATE user SET name = :name, password = :password, email = :email WHERE id = :recordID";
            $stmt 	=	$pdo->prepare($sql);
            $stmt->bindParam(':name', $name, PDO::PARAM_STR);
            $stmt->bindParam(':password', $password, PDO::PARAM_STR);
			$stmt->bindParam(':email', $email, PDO::PARAM_STR);
            $stmt->bindParam(':recordID', $recordID, PDO::PARAM_INT);
            $stmt->execute();

            echo json_encode('Congratulations the record ' . $name . ' was updated');
         }
         // Catch any errors in running the prepared statement
         catch(PDOException $e)
         {
            echo $e->getMessage();
         }

      break;



      // Remove an existing record in the technologies table
      case "delete":

         // Sanitise supplied record ID for matching to table record
         $recordID	=	filter_var($obj->recordID, FILTER_SANITIZE_NUMBER_INT);

         // Attempt to run PDO prepared statement
         try {
            $pdo 	= new PDO($dsn, $un, $pwd);
            $sql 	= "DELETE FROM user WHERE id = :recordID";
            $stmt 	= $pdo->prepare($sql);
            $stmt->bindParam(':recordID', $recordID, PDO::PARAM_INT);
            $stmt->execute();

            echo json_encode('Congratulations the record ' . $username . ' was removed');
         }
         // Catch any errors in running the prepared statement
         catch(PDOException $e)
         {
            echo $e->getMessage();
         }

      break;
      */
   }
?>

This is missing in your create route…

} else {
$sql = “INSERT INTO user(id, name, password, email) VALUES(:id, :name, :password, :email)”;
$stmt = $pdo->prepare($sql);
$stmt->bindParam(’:id’, $id, PDO::PARAM_STR);
$stmt->bindParam(’:name’, $name, PDO::PARAM_STR);
$stmt->bindParam(’:password’, $password, PDO::PARAM_STR);
$stmt->bindParam(’:email’, $email, PDO::PARAM_STR);
$stmt->execute();
// No response here…
}

But I woud send something back like

echo '{"status":"error", "message": "Your message"}’;

If you have something like this you can work with it on the client side.

Still getting the error and answer tab is empty :frowning:

The response header says the following

content-encoding:gzip
content-type	:text/html; charset=UTF-8
date	:Wed, 27 Dec 2017 19:21:59 GMT
server:awex
x-content-type-options	:nosniff
X-Firefox-Spdy	:h2
x-request-id	:c5ecc30d9c6695f1f39cad2f3285e6a9
x-xss-protection	:1; mode=block

Don’t know if it’s helpful.

had to add this in my webserver php files…

header("Access-Control-Allow-Origin: *");