2

I am having issues with format of angular app form post. Here is the html code for the form

                <form class="col s8 center">
                    <h4>
                        {{greeting}}
                    </h4>
                  <div class="row">
                    <div class="input-field col s12">
                      <input id="email" type="email" class="validate" ng-model="user.email">
                      <label for="email">Email</label>
                    </div>
                    <div class="input-field col s12">
                      <input id="password" type="password" class="validate" ng-model="user.password">
                      <label for="password">Password</label>
                    </div>
                  </div>
                  <div class="row">
                      <div class="col s12">
                        <input type="submit" class="waves-effect waves-light btn pink" style="width:100%" value="Login" ng-click="login(user)">
                      </div>
                  </div>
                </form>

here is the angular login function that i am using

$scope.login = function(user) {
  console.log(user.email);
  $http({
    url: baseDomain + 'auth/login/',
    method: "POST",
    data: user,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  }).then(function(response) {
    $window.localStorage.token = response.data.token;
  }, function(response) {
    console.log(response);
  });
};

Now the function is being called properly but the post data is like

{"password":"d","email":"d@s"}:""

What is the corrct way to do the same and where am i going wrong ?

** Edit **

the post data is taken from firefox dev tool inspector.

3
  • I can't understand where is the problem. do you mean that your web api doesn't receive the request ? Commented Apr 23, 2016 at 8:51
  • Its not in proper format. Look at the post data, what i want as key are username and password . The application is using {"password":"d","email":"d@s"} as key in whole Commented Apr 23, 2016 at 8:52
  • so simply said $_POST["email"] is empty but the value is in $_POST['{"password":"d","email":"d@s"}'] Commented Apr 23, 2016 at 8:53

2 Answers 2

1

You need to serialize data properly as "key=vale&key1=value2" string for this application/x-www-form-urlencoded content type. For this you can use built-in service $httpParamSerializer:

$http({
  url: baseDomain + 'auth/login/',
  method: "POST",
  data: $httpParamSerializer(user),
  headers: {
    'Content-Type': 'application/x-www-form-urlencoded'
  }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah i tried last time, apparently need to wait sometime, will do it now :)
1

I think you should refer to this solution:

https://stackoverflow.com/a/24964658/3351642

which state that

By default, the $http service will transform the outgoing request by serializing the data as JSON and then posting it with the content- type, "application/json". When we want to post the value as a FORM post, we need to change the serialization algorithm and post the data with the content-type, "application/x-www-form-urlencoded".

so you should do that:

$http({
    method: 'POST',
    url: url,
    headers: {'Content-Type': 'application/x-www-form-urlencoded'},
    transformRequest: function(obj) {
        var str = [];
        for(var p in obj)
        str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        return str.join("&");
    },
    data: {username: $scope.userName, password: $scope.password}
}).success(function () {});

1 Comment

although i am pretty sure that this answer is correct, but i went with the one above because it is more efficient and i need to write less code. But anyways i will give you an upvote :D

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.