0

The below code is angular2 form submission code,i am sending a test username and password

logForm(formData){
console.log('Form data is ', formData.title);
 var link = 'http://test/index.php/api/userAuth';
 var headers = new Headers();
 headers.append("Content Type","application/json"); 

    var data = JSON.stringify({
      username:'user',
      password:'user123'
    }); 

    this.http.post(link,data,headers)
    .subscribe(data => {
      console.log(data.json());
    });
}

The PHP side is in codeigniter using REST architecture, php code is given below

function userAuth_post() {

    if (($this->post('username') == '') || ($this->post('password') == '')) {
        $this->response(array('message' => 'Network Error! Try Again!!!'), 200);
    }else{
        $this->response(array('message' => 'Success'), 200);
    }
}

Now i am receiving "Network Error! Try Again!!!", i should be getting "Success".

code_boy
  • 43
  • 5
  • 2
    http://stackoverflow.com/q/15485354/1220930 – Timurib Nov 28 '16 at 14:30
  • Thanks for the answer, got it working using $postdata = file_get_contents("php://input"); if (isset($postdata)) { $request = json_decode($postdata); $userName = $request->username; $userPass = $request->password; – code_boy Nov 28 '16 at 14:49

2 Answers2

0

Edit: I missed the point that it's input stream

Try this instead:

$this->input->input_stream('field');

if ( ! $this->input->input_stream('username') || ! $this->input->input_stream('password')) {
    $this->response(array('message' => 'Network Error! Try Again!!!'), 200);
}else{
    $this->response(array('message' => 'Success'), 200);
}

this is how you can get php://input in CodeIgniter:

$this->input->raw_input_stream;

Read more: Here

Vladimir
  • 1,602
  • 2
  • 18
  • 40
  • i tried using $this->input->post('field'); still not receiving the username and password at the backend, i am using REST architecture – code_boy Nov 28 '16 at 14:39
  • $postdata = $this->input->raw_input_stream; if (isset($postdata)) { $request = json_decode($postdata); $userName = $request->username; $userPass = $request->password; } This worked – code_boy Nov 29 '16 at 07:17
0

I got it working by using

function userAuth_post() {

    $postdata = file_get_contents("php://input");
    if (isset($postdata)) {
         $request = json_decode($postdata);
         $userName = $request->username;
         $userPass = $request->password;
     }
    if (($userName == '') || ($userPass == '')) {
        $this->response(array('message' => 'Network Error! Try Again!!!'), 200);
    }else{
    $this->response(array('message' => 'Success'), 200);
  }
}

In Codeigniter we can use

$postdata = $this->input->raw_input_stream;

Don't know whether this is the best answer, but i am going with it now, would be good if someone can correct it if there is an better solution.

code_boy
  • 43
  • 5
  • Please also try it like this: $this->input->input_stream('username'); or $postdata = $this->input->raw_input_stream; and then $username = $postdata['username']; – Vladimir Nov 28 '16 at 15:56
  • @Vladimir tried both but didnt work $userName = $this->input->input_stream('username'); $userPass = $this->input->input_stream('password'); $postdata = $this->input->raw_input_stream; $username = $postdata['username']; $userPass = $postdata['password']; – code_boy Nov 29 '16 at 06:50