0

I am new to ionic2 and a bit confused about the best approach to retrieve the request send from app to a php file..

I am integrating my app with laravel PHP framework .. Currently I am able to access request if using

$postdata = file_get_contents("php://input");

Here I am posting data to php server using ionic2:

this.http.post('http://localhost/lara/blog/public/api/apis/login', JSON.stringify({email: email,password: password}), { headers: headers }).map(res => res.json()).subscribe(data => {

});

public function apiLogin(Request $request)
  • Actually the issue is that when using postman I am able to get request in $request but when sending from ionic2 I am not getting it in $request but getting in $postdata..

  • So whats the correct way to retrieve in PPH or if I am sending it in a wrong way ..

    Can any one please suggest?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Narendra Vyas
  • 717
  • 4
  • 9
  • 26
  • "i am not getting it in $request but getting in $postdata." what does it mean? are u using another variable $request in php? – Suraj Rao Dec 13 '16 at 06:22
  • Yes actually in laravell on using postman i can easily get request in $request as :public function apiLogin(Request $request) { print_r($request); } ....... but when sending request using ionic i am not getting it in $request .. insteed i have to use $postdata = file_get_contents("php://input"); ..... so is this fine or its not a good thing i am a bit confused – Narendra Vyas Dec 13 '16 at 06:31
  • what are the headers u used in postman and ionic 2? – Suraj Rao Dec 13 '16 at 06:33
  • In ionic : header('Access-Control-Allow-Origin: *'); and in postman: content type : json – Narendra Vyas Dec 13 '16 at 06:34
  • that is server side header,not in ionic – Suraj Rao Dec 13 '16 at 06:35
  • ohk actually i think the issue is that i am not setting content type json in ionic2 .... actually i tryied to do that but it rendered some error. – Narendra Vyas Dec 13 '16 at 06:42

1 Answers1

0

You are getting in $postData because you are basically sending json data. Check here about reading json post request

you are setting body as:

JSON.stringify({email: email,password: password})

so header becomes: 'Content-Type': 'application/json'

You can set

'Content-Type':'application/x-www-form-urlencoded'

and body as:

"email="+email+"&password="+password
Community
  • 1
  • 1
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103