0

I'm getting issue in angular 4 .... I am building hybrid app using ionic 3.

I have to use php as server side language for APIs, I am having issue in posting variables to http.post() request ...

variables are not passed to php file.

Method I'm using to call API from home.ts is:

var json = {var1: 'test'};

var params = 'json='+json;

let headers = new Headers();

headers.append('Content-Type', 'application/json');

http.post("http://example.com/infoarray.php", params, headers ).subscribe (re => {
console.log(re)
})

I'm able to get response from API just fine but the problem is I can't pass the value of variable 'var1' to my API on infoarray.php I'm encoding the request to json ... but didn't get variables

Md Hasan Ibrahim
  • 1,808
  • 17
  • 27
Rizwan Saleem
  • 186
  • 2
  • 13
  • how you are accessing var1 in server side? – Dinesh undefined Jun 22 '17 at 07:48
  • yes, I'm accessing $_POST['var1'] to php file – Rizwan Saleem Jun 22 '17 at 07:50
  • 1
    access it using **json** not var1. later json.var1 you can get the value of var1 – Dinesh undefined Jun 22 '17 at 07:55
  • Generically. Read this: http://victorblog.com/2012/12/20/make-angularjs-http-service-behave-like-jquery-ajax/ and you can use that as a base to create something for Angular 4. I created a similar conversion function for Angular4 / ColdFusion; which could be converted back for PHP usage--I think the array handling would need to change: https://github.com/Reboog711/AngularToColdFusionHTTPModule – JeffryHouser Jun 22 '17 at 12:46

5 Answers5

1
  1. Check your PHP code, may be you are missing to get JSON content from Post.

    $data = json_decode(file_get_contents('php://input'), true);
    print_r($data);
    echo $data["param"];
    
  2. Otherwise try to add params in form data

    let headers      = new Headers({ 'Content-Type': 'application/x-www-form-urlencoded' });
    let options      = new RequestOptions({ headers: headers }); // Create a request option
    let url = "SERVER_URL";
    let body = 'param1='+value1+'&param2=' +value2;
    
    this.http.post(url, body, options)
    
Nadeem Yousaf
  • 553
  • 8
  • 27
0

Use JSON.stringify() method while processing the params:

var json = {var1: 'test'};

var params = 'json='+JSON.stringify(json);
Md Hasan Ibrahim
  • 1,808
  • 17
  • 27
0

Your params = 'json='+json will actually result to this: "json=[object Object]". That's because you didn't stringify the object first, which is done as so:

var params = 'json=' + JSON.stringify(json); // "json={"var1":"test"}"
Rax Weber
  • 3,551
  • 15
  • 30
0

Use JSON.stringify() and put your headers in RequestOptions class

let headers = new Headers();
    headers.append('Content-Type', 'application/json');
let requestOpt = new RequestOptions({ headers: headers });
    const bodyStr = JSON.stringify({var: value});

this.http
    .post(this.vocabularyPostUrl, bodyStr, requestOpt)
Yordan Nikolov
  • 2,392
  • 10
  • 14
0

You have to stringify the object. Otherwise you will end in getting "[object][object]" rather than "{var1: 'test'}".

JSON.stringify(json);