Asked  7 Months ago    Answers:  5   Viewed   37 times

I have a form with the tag ng-submit="login()

The function gets called fine in javascript.

function LoginForm($scope, $http)
    $['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';

    $    = "";
    $scope.password = "1234";

    $scope.login = function()
        data = {
            'email' : $,
            'password' : $scope.password

        $'resources/curl.php', data)
        .success(function(data, status, headers, config)
            console.log(status + ' - ' + data);
        .error(function(data, status, headers, config)

I am getting a 200 OK response back from the PHP file, however, the returned data is saying that email and password are undefined. This is all the php I have

$email = $_POST['email'];
$pass  = $_POST['password'];
echo $email;

Any idea why I am getting undefined POST values?


I wanted to point out since this seems to be a popular question (yet it is old), .success and .error have been deprecated and you should use .then as @James Gentes pointed out in the commments



angularjs .post() defaults the Content-type header to application/json. You are overriding this to pass form-encoded data, however you are not changing your data value to pass an appropriate query string, so PHP is not populating $_POST as you expect.

My suggestion would be to just use the default angularjs setting of application/json as header, read the raw input in PHP, and then deserialize the JSON.

That can be achieved in PHP like this:

$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;
$pass = $request->password;

Alternately, if you are heavily relying on $_POST functionality, you can form a query string like and send that as data. Make sure that this query string is URL encoded. If manually built (as opposed to using something like jQuery.serialize()), Javascript's encodeURIComponent() should do the trick for you.

Wednesday, March 31, 2021
answered 7 Months ago

You need to convert your request to php known format.
I use jQuery $.param method to build it. You can write your own.

app.config(['$httpProvider', function($http) {  
    $['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
    $http.defaults.transformRequest = function(data) {
            return $.param(data);

urlencoded is followed simple format:



Saturday, May 29, 2021
answered 5 Months ago

Updated, this is code that was just tested with php and Apache - and it works. I also changed your server.php file like below. The file was created based on AngularJS Hub's Server Calls sample. The same source was used to create mainController.js' $ method call so that it successfully posts data to the server.

Screenshot (after submit)

enter image description here



   $result = "POST request received!";

  if (isset($_GET["name"]))
    $result .= "nname = " . $_GET["name"];

  if (isset($_GET["email"]))
    $result .= "nemail = " . $_GET["email"];

  if (isset($HTTP_RAW_POST_DATA))
    $result .= "nPOST DATA: " . $HTTP_RAW_POST_DATA;

  echo $result;



   <!DOCTYPE html>
    <html lang="en" xmlns="">
        <meta charset="utf-8" />

        <body ng-app="mainModule">
            <div ng-controller="mainController">
                <form name="personForm1" validate ng-submit="submit()">
                    <label for="name">First name:</label>
                    <input id="name" type="text" name="name" ng-model="" required />
                    <br />
                    <br />
                    <label for="email">email:</label>
                    <input id="email" type="text" name="email" ng-model="" required />
                    <br />
                    <br />
                    <button type="submit">Submit</button>
                <br />

            <script src=""></script>
            <script src=""></script>
            <script src="mainController.js"></script>
            <!--<script type="text/javascript" src="script/parsley.js"></script>
            <script src="script.js"></script>-->



angular.module("mainModule", [])
  .controller("mainController", function ($scope, $http)
  $scope.person = {};

  $scope.serverResponse = "";

  $scope.submit = function ()

      console.log("form submit");

      var params = {
          name: $,
          email: $

      var config = {
          params: params

      $"server.php", $scope.person, config)
      .success(function (data, status, headers, config)
          console.log("data " + data + ", status "+ status + ", headers "+ headers + ", config " + config);
          $scope.serverResponse = data;
      .error(function (data, status, headers, config)
      { console.log("error");
          $scope.serverResponse = "SUBMIT ERROR";

  });// JavaScript source code

Alternative way, with JSON handling:


     /* code source: */
     $data = array();
     $json = file_get_contents('php://input'); // read JSON from raw POST data

     if (!empty($json)) {
        $data = json_decode($json, true); // decode




Screenshot (after submit)


Saturday, May 29, 2021
answered 5 Months ago

You could set the key by index notation of array as you want to set user['token'] dynamically


var formData = { email : email, password : password};

formData[user['token']] = token; //assuming user & token object has already defined

//user[token] : token

$'', formData).then(function(results) {
Saturday, May 29, 2021
answered 5 Months ago

Your request does not contain a URL parameter named "data", therefore request.getParameter("data") returns null and you get the NullPointerException.

You try to send a Javascript object via URL parameters which does not go well with non-shallow objects.

I would recommend to send the data as request payload:

JsonObject obj = (JsonObject) parser.parse(request.getReader());

On the client you need to make sure that your data is sent as proper JSON:

        method : 'POST',
        url : 'login',
        contentType: 'application/json',
        data : JSON.stringify($scope.user),
Tuesday, August 3, 2021
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :