Use Http Post in ionic

@shivani8710 your code work properly for me.but i don’t know why why showing an error .

@shivani8710

Please try again.

this.http.post("Your URL",JSON.stringify(postParams))
      .subscribe(data => {
        this.mydata =data.json()
        let response = this.mydata.message;
        alert(response);
      }, error => {
        
        // Error getting the data
        console.log(error);
      });
  }

@shivani8710

Here the whole function just copy and past

UserRegister(){
      let postParams =  {
      "name": this.name,
      "email": this.email,
      "phone": this.phone,
      "password": this.password,
      "photos":this.photos
    } 
    
    this.http.post("URL",JSON.stringify(postParams))
      .subscribe(data => {
        this.mydata =data.json()
        let response = this.mydata.code;
        alert(response);

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

Heres how i do it:
on .ts

this.http.post( this.globalServices.api_url + '/item/item/'+this.uid, JSON.stringify(body), {headers: this.http_headers}  )
        .map(res => res.json())
        .subscribe(data => {
          this.navCtrl.pop();
        });

on .php (server side PHP), you have to enable CORS and set what method you’ll access. You’ll have to implement some sort of security though.

   // Allow from any origin
    if (isset($_SERVER['HTTP_ORIGIN'])) {
        // Decide if the origin in $_SERVER['HTTP_ORIGIN'] is one
        // you want to allow, and if so:
        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']))
            // may also be using PUT, PATCH, HEAD etc
            header("Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS");         

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

        exit(0);
    }

run the code above before you output your json data:

header('Content-Type: application/json');
echo json_encode(array('key'=>'value'));

I’m getting response in string

  .subscribe(data => {
          alert(data['_body'][0]);
      },
      err => {
          alert("Error : " + err);
      });

when I’m doing this that time data[ ‘_body’ ][0] returns only '{ '
means it returns string as response…But Response send me a JSON array as response which I got as string

you can use RxJs to map the returned data to jsob object like this:

post(..).map(res => res.json()).subscribe(..)

JavaScript is a terrible language. Since it has no actual access protection, there is a convention that leading underscores mean “this is private, don’t touch it”. Please follow it.

unable to send parameter but in post line it will show all parameter.

im using this. and its working for me