0

When Add new User is clicked there will be a modal pop-up with form that include a text field and a checkbox.

When I click the create button it does not post the data to the API and also the modal pop-up remain there without closing.

I want the data to be posted to the API (insert), the modal pop-up to close after clicking on create and also the new user to be added to the List.

Please here is my code

$scope.createUser = function(){
        $http({
          method  : 'POST',
          url     : 'mydomain.com/api/CreateUser',
          data    : $scope.newUser,
          headers : {'Content-Type': 'application/x-www-form-urlencoded'} 
         })
          .success(function() {});
  };


<div class="modal-body">

                  <form ng-submit="createUser ()">
                        <div class="form-group">
                            <div class="row" style="padding-left: 30px;">
                              <div class="col-xs-2">
                                <label>User Name</label>
                              </div>
                              <div class="col-xs-10">
                                <input type="text" class="form-control" placeholder="" ng-model="newUser.name">
                              </div>
                            </div>

                        </div>
                        <div class="form-group">
                            <div class="row" style="padding-left: 30px;">
                              <div class="col-xs-2">
                                <label>&nbsp;</label>
                              </div>
                              <div class="col-xs-10">
                                <input type="checkbox" name="active" ng-model="newUser.active"> &nbsp; Is active?
                              </div>
                            </div>

                        </div>


                        <div class="modal-footer">
                            <button class="btn btn-info" type="submit">Create</button>
                            <button class="btn btn-warning" type="button" ng-click="cancel()">Cancel</button>
                        </div>
                </form>
        </div>
2
  • How are you retrieving the data in the API file? Commented Mar 14, 2016 at 15:46
  • I retrieve data using http get, but couldn't post to it Commented Mar 14, 2016 at 15:51

1 Answer 1

2

Try this:

$http.post('mydomain.com/api/CreateUser', $scope.newUser).success(function(response) {
     console.log(response);
});

or

$http({
    method: 'POST',
    url: 'mydomain.com/api/CreateUser',
    headers: 
    {
        'Content-type': 'application/x-www-form-urlencoded',
    },
    data: 
        'name=' + $scope.newUser.name + 
        '&active=' + $scope.newUser.active
}).success(function(data, status) {
         console.log(data);
});
Sign up to request clarification or add additional context in comments.

5 Comments

@user3055606 or you can try if the edited version works for you
I'm not in possession of the API, I'm just to consume it. Can you help post a working example on plnkr using an xml as database?
You should be able to provide info from the API
Yes i can get info about each field from the api but not example
Could you provide more info what you are using otherwise I can't help you.

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.