0

I have following html page :

<!DOCTYPE html>
<html>

<head>
  <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.5/angular.min.js"></script>
  <script src="scriptAssign.js"></script>
</head>

<body ng-app="myapp">

  <div ng-controller="MyController">
    <form ng-submit="submitForm()" method="POST">
      ATTID :
      <input type="text" name="firstName" ng-model="myForm.firstName">
      <br/> Role :
      <select ng-model="selectedRole" ng-options="myForm.roleName for myForm in role"></select>
      <br/>
      <input type="submit" id="submit" value="Submit" />
    </form>
    <div>
      {{myForm.firstName}} {{selectedRole.roleName}}
    </div>
  </div>

  <script>
  </script>

</body>

</html>

My javascript file is as follows :

angular.module("myapp", [])
.controller("MyController", ['$scope', '$http', function($scope, $http) {
    $scope.myForm = {};
    $scope.role = [{ roleName :"A"},{roleName : "B"},{ roleName : "C"}];
    $scope.submitForm = function() {
        $http.post('/services/poc/add').success(function(data) {
            $scope.myData = data;
        });
    }
}]);

i am getting error as

HTTP Status 400 - type Status report message description The request sent by the client was syntactically incorrect. Apache Tomcat/7.0.32

2
  • also change ng-model="selectedRole" to ng-model="myForm.selectedRole" Commented Apr 18, 2017 at 15:27
  • are you really using angular 1.2.5 in your app? That version was last updated in October, 2015. Commented Apr 18, 2017 at 15:34

2 Answers 2

2

You're not sending a payload in your $http.post; your service is likely expecting one.

You might also want to adjust your ng-options and ng-model on that select.

HTML:

// use a different alias (r) instead of 'myForm' for defining options
<select ng-model="myForm.selectedRole" ng-options="r.roleName for r in role"></select>

JS:

    $http.post('/services/poc/add', $scope.myForm).success(function(data) {
        $scope.myData = data;
    });
Sign up to request clarification or add additional context in comments.

2 Comments

If i have 1000 fields in my form,so this alias "myForm" actually helps in retrieving the values of field.Is this why we define alias.just curious as i am new to angularjs.
The only reason I suggested a different alias is because myForm was already being used as the model for collecting your form inputs (i.e. ng-model="myForm.firstName"). r on the other hand is just an arbitrary iterator scoped to that particular ng-options statement; lessens your chance of causing an unexpected conflict.
0

I don't know what your API looks like, so it is hard to say; however, the data you POST needs to be a parameter to the post method.

$http.post('/services/poc/add', $scope.dataToPost).then(
  function(response) {
    console.log("Success!");
  },
  function(error) {
    console.log("Something went wrong!");
  });

Remember that the posting happens by the method post, which just returns a promise you're tapping into with then. The success callback is the code that gets executed when the response is successfully returned.

Here are the docs - angular's are pretty good: https://docs.angularjs.org/api/ng/service/$http#post

4 Comments

So if i have to attach the fields in place of $scope.data in argument of $http.post,what i would have to do?
Like I said, I don't know anything about your API so I can't really say. If it is an object (e.g. var data = {fruit: "apple", name: "Nick"} you could post that by passing the parameter to POST like $http.post('/services/poc/add', data). Or even $http.post('/services/poc/add', {fruit: "apple", name: "Nick"})
@UtkarshSaraf looking more closely at the question, it looks like SteamDev answered the question. myForm is an object, so you'd pass it in like I did in my above comment (as steamDev did). you should accept his answer.
ohh..yes.actually at time of asking this question,that was not visible in my post.Will surely accept it.Thanks Nick for highlighting.

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.