2

Here is my function AngularJS

'use strict';

 app
.factory('userProvider', function ($rootScope , $http) {

  function logIn(user) {
    var url='http://127.0.0.1:100/suitecrm/service/rest.php';

    /*$http.post(url,user)
      .success(function (response) {
      console.log(response);
        alert(user['username']);  //data is displayed normally here
        alert(user['password']);  //data is displayed normally here
      });
    */

    $http({
      method: 'POST',
      url: 'http://127.0.0.1:100/suitecrm/service/rest.php',
      //data: 'username: user['username'] , password: user['password'] }
      data: { username: user['username'] , password: user['password']}

    }).then(function successCallback(response) {
      console.log(response);

    }, function errorCallback(response) {
      // called asynchronously if an error occurs
      // or server returns response with an error status.
    });
  }

  return {
    logIn: logIn
  }
});

I want to retrieve my two variable username and password in my file rest.php. Here is the code where i get username and password

$username =$_POST['username'];
$password =$_POST['password'];

But when i open the console an error is displayed : username & password not defined in rest.php so how can i retrieve data in my file rest.php correctly.

1
  • 1
    This is the error in the console : <b>Notice</b>: Undefined index: username in <b>C:\Users\hp1\Desktop\Stage\SugarCrm Thinline\suitecrm\service\rest.php</b> on line <b>3</b><br /> <br /> <b>Notice</b>: Undefined index: password in <b>C:\Users\hp1\Desktop\Stage\SugarCrm Thinline\suitecrm\service\rest.php</b> on line <b>4</b><br /> Commented Aug 22, 2016 at 16:05

1 Answer 1

3

I think that unless you are submitting via a form method="POST" you need to read/parse the raw input stream.

Try with:

$request = json_decode(file_get_contents('php://input'));

You can put this at the start of your PHP script. $request will then be an stdClass object with the data as properties.

$username = $request->username;
$password = $request->password;

Alternatively, if you prefer to work with it as assoc array, use:

$request = json_decode(file_get_contents('php://input'), TRUE);

and access data like:

$username = $request['username'];
$password = $request['password'];
Sign up to request clarification or add additional context in comments.

1 Comment

Updated answer with more detail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.