Values are properly inserted into db but nothing show in respose


#1

here is my code
I’m trying to insert data into the db Values are properly inserted into db but nothing show in respose
this.http.post(“http://www.domian.com/multiplefield/insert.php”,
JSON.stringify(this.post_to_server)).subscribe(data => {
let server = data.json();

    let respose = server.name;
    alert(respose)
  });

#2

try to place few console.log and see first if data holds anything.
data=>{
console.log('data: ",data);
let server = data.json();
console.log('Server: ",server);
.
.
.
}


#3

no. data is not holding anything


       this.http.post("http://www.mydomain.com/multiplefield/insert.php",JSON.stringify(this.post_to_server))
      .subscribe(data => {
      console.log("data",data)
      let server = data.json();
      console.log("Serve",server);

      }, error => {
        // Error getting the data
        console.log(error);
      });

but
console.log(error)
showing this error
Object { _body: error, status: 0, ok: false, statusText: "", headers: {…}, type: 3, url: null }
how can i fixed that?
please help me


#4

The error code show status as 0. A normal response from HTTP call should be 200.
What do you get if you call the http://www.mydomain.com/multiplefield/insert.php directly?
Can you check on the server side, what the insert.php product?
If you run it using ‘ionic serve’, you can use tcpdump to trace the communication.


#5

i’m call the api directly
below is my php code

 <?php 
     // Allow from any origin
    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
    }
    if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {

        if (isset($_SERVER['HTTP_ACCESS_CONTROL_GET_METHOD']))
            header("Access-Control-Allow-Methods: GET, POST, OPTIONS");         

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

        exit(0);
    }
$con=mysqli_connect("localhost","username","password","dbnmae");


$postdata = file_get_contents("php://input");  
        $obj = json_decode($postdata,TRUE);

        foreach($obj as $key)
        { 
               
                $query = "insert into test (name,address,doc_contact) values ('".$key['name']."','".$key['address']."','".$key['contact']."')";
                $result = mysqli_query($con,$query);    //execute $query with mysqli_query
        }

        if($result) {
        $data = [ 'Status' => '1'];

    } else {
        $data['status']='0';
        $data['message']='Failed...';
    }

    echo json_encode($data);

 ?>

#6

Create a dummy php call that always return a valid results with status 200.
Use this one in your ionic this.http.post call, and see if you are getting the valid results.
( Don’t try to debug both backend and frontend at the same time ).