How to call angular http post equivalent to this jquery

In my Rest.in.php

private function set_headers(){
        header("HTTP/1.1 ".$this->_code." ".$this->get_status_message());
        header("Content-Type:".$this->_content_type);
    }

I used jquery and got response

    <script>
    $(document).ready(function(){
        $("button").click(function(){
            alert("haha");
            $.post("http://localhost/fudline/access/hotels/loadplaces",
            {
              district: "Malappuram"
            },
            function(data,status){
                alert(data);
                console.log(data);
                console.log(status);
            });
        });
    });
    </script>

How to call angular http post for this...I tried below code and not going to success() function..I need to use angular js because I want use ng-repeat.

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope, $http) {
    $scope.loadPlace = function(x) {
      alert(x);
      $http({
        method: 'POST',
        url: 'http://localhost/fudline/access/hotels/loadplaces',
        data: { district: "Malappuram"},
        headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        })
      .success(function(data, status, headers, config) {
          alert("haha");
          alert(data);
          $scope.places = data;
      }).error(function(data, status, headers, config) {
          //
      });



    }
</script>

Answers 1

  • You need to serialize the data yourself as the $http default is to serialize as json not urlencoded which is the default for $.ajax

    Inject $httpParamSerializerJQLike service and change:

    data: { district: "Malappuram"},
    

    To

    data: $httpParamSerializerJQLike({ district: "Malappuram"}),
    

Related Articles