# HTTP Request ends in Status:0 for URL: null

**URL:** <https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134>\
**Category:** ionic-v3\
**Created:** [December 21, 2017, 7:32pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134 "2017-12-21T19:32:24Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 21, 2017, 7:32pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/1 "2017-12-21T19:32:24Z")

</div>

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/](https://de.000webhost.com/).

So here is the code of the http part:

```auto
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](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…

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 22, 2017, 1:44pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/2 "2017-12-22T13:44:24Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [December 22, 2017, 2:34pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/3 "2017-12-22T14:34:53Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 23, 2017, 1:16pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/4 "2017-12-23T13:16:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Sujan12](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/sujan12/32/108395_2.png) [@Sujan12](https://forum.ionicframework.com/u/Sujan12)\
**Post date:** [December 23, 2017, 3:02pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/5 "2017-12-23T15:02:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 25, 2017, 1:40pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/6 "2017-12-25T13:40:48Z")

</div>

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

Screen of network panel:

 ![Unbenannt](https://global.discourse-cdn.com/ionicframework/original/3X/0/5/05b988c2dcfb4d3b043d82397745c8792e56ca25.jpg)

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [December 25, 2017, 3:22pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/7 "2017-12-25T15:22:12Z")

</div>

Perhaps the error is on the server side.

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

And so on…

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 27, 2017, 5:00pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/8 "2017-12-27T17:00:03Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [December 27, 2017, 5:50pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/9 "2017-12-27T17:50:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 27, 2017, 7:01pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/10 "2017-12-27T19:01:37Z")

</div>

PHP code would be:

```auto
<?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;
      */
   }
?>

```

---

<div class="post-metadata">

**Author:** ![Jacktoolsnet](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/jacktoolsnet/32/107318_2.png) [@Jacktoolsnet](https://forum.ionicframework.com/u/Jacktoolsnet)\
**Post date:** [December 27, 2017, 7:05pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/11 "2017-12-27T19:05:45Z")

</div>

> [@HansMartin](#):
>
> echo json\_encode(‘Congratulations the record ’ . $name . ’ was updated’);

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.

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 27, 2017, 7:26pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/12 "2017-12-27T19:26:02Z")

</div>

Still getting the error and answer tab is empty ☹

The response header says the following

```auto
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.

---

<div class="post-metadata">

**Author:** ![HansMartin](https://avatars.discourse-cdn.com/v4/letter/h/57b2e6/32.png) [@HansMartin](https://forum.ionicframework.com/u/HansMartin)\
**Post date:** [December 28, 2017, 5:23pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/13 "2017-12-28T17:23:19Z")

</div>

had to add this in my webserver php files…

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

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 9:57pm UTC](https://forum.ionicframework.com/t/http-request-ends-in-status-0-for-url-null/116134/14 "2021-07-19T21:57:52Z")

</div>

Closed on July 19, 2021, 9:57pm UTC


