Error while posting data from angular js form

强颜欢笑 提交于 2019-12-03 16:32:32

Two changes:

  1. Use the 'ng-submit' event.
  2. Put the ng-models inside an object themselves so you can just send the object in the post.

HTML:

<div ng-controller="UserController">
  <form ng-submit="createUser()">
    <input type="text" ng-model="user.name">
    ...
    <input type="email" ng-model="user.email">
    ...
  </form>
</div>

JS:

function UserController($scope, $http) {
  $scope.user = {};
  $scope.createUser = function() {
    $http.post('/createUser', $scope.user);
  }
}

I just solved this by adding header information in the http post itself below is the code

$scope.createUser = function() {
$http({
method: 'POST',
url: '/createUser',
data: 'name=' + $scope.user.name + '&email=' +$scope.user.email,
headers: {'Content-Type': 'application/x-www-form-urlencoded'}
})

}

If anyone else have any other better approach please post your suggestions.

Well I also had similar problems, data was being posted to the server correctly but couldn't receive in the POST data with normal $_POST or $_REQUEST

However, following worked for me

$data = json_decode(file_get_contents("php://input"));

This will be late to answer. Anyway, I assume this will help someone else and therefore, I post my answer.

If you want to read the body of request when the request is submitted as POST , you may get it with the reader of HttpRequestas following.

      BufferedReader reader = request.getReader();
      String line = null;
      while ((line = reader.readLine()) != null)
      {
        sb.append(line);
      }

 String requestBody = sb.toString();

Hope this will helpful.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!