Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I'm having two buttons, each button has ng-click event, the click event calls one method, the method has one argument.

I wish to pass the value of the argument to the Service Side via HTTP POST from AngularJS Controller.

The AngularJS Function CustomerGender(index), I wish to pass the index value to the Service.

<div ng-app="Customer" ng-controller="CustomerCtrl">
    <button ng-click="CustomerGender('Male')">List of Male Customers</button>
    <button ng-click="CustomerGender('FeMale')">List of Female Customers</button>
</div>

The AngularJS Source Code:

Note: The URL http://www.w3schools.com/angular/customers.php is a Sample URL

var CustomerApp = angular.module('Customer', []);
CustomerApp.controller('CustomerCtrl', function($scope, $http, $cacheFactory) {

    $scope.CustomerGender = function(index){

        var request = $http({
            method: "post",
            url: "http://www.w3schools.com/angular/customers.php",
            data: {
                token: index
            },
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        });
        request.success(function (response) 
        {
            $scope.data = response.records;
        });
    };
});

The Function is not working. If I set token: 'Male' directly, in Controller, then it pass the value to the Service, If i set token: index, then it can't pass the value to the Service. Kindly assist me.

share|improve this question
    
What exactly is not working? Do you have any error messages from the console to share? – Brendan Green Jan 25 at 4:06
    
If I set token: 'Male', in Controller, then it pass the value to the Service, If i set token: index, then it can't pass the value to the Service. – B.Balamanigandan Jan 25 at 4:10

When I try to use your code to post to the w3schools link it blocks the request. It seems to work fine when I try a different endpoint like https://httpbin.org/post though.

Here is a working plunker.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.