Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS - $http.post send data as json

I'm working on autocomplete directive with angularjs but having some issues.

I have a form which have an autocomplete input. When i type something there, the term variable is sent as JSON:

enter image description here

But, when i use the same function (from different angular controller, but the same function) in another form the term variable sent perfectly and the autocomplete works fine:

enter image description here

Here is my angular function:

$scope.getCustomers = function (searchString) {
    return $http.post("/customer/data/autocomplete",
        {term: searchString})
        .then(function (response) {
            return response;
        });
};

What do you think is wrong?

like image 208
Jhonatan Sandoval Avatar asked Jul 03 '14 04:07

Jhonatan Sandoval


People also ask

Can we send JSON object in post request?

Specify the POST data: As per the HTTP specification for a POST request, we pass data through the message body. Using requests, you'll pass the payload to the corresponding function's data parameter. Data can be anything including JSON, dictionary, a list of tuples, bytes, or a file-like object.

How do we pass data and get data using HTTP in angular?

Use the HttpClient.get() method to fetch data from a server. The asynchronous method sends an HTTP request, and returns an Observable that emits the requested data when the response is received. The return type varies based on the observe and responseType values that you pass to the call.

What is $HTTP in AngularJS?

$http is an AngularJS service for reading data from remote servers.


3 Answers

Use JSON.stringify() to wrap your json

var parameter = JSON.stringify({type:"user", username:user_email, password:user_password});
    $http.post(url, parameter).
    success(function(data, status, headers, config) {
        // this callback will be called asynchronously
        // when the response is available
        console.log(data);
      }).
      error(function(data, status, headers, config) {
        // called asynchronously if an error occurs
        // or server returns response with an error status.
      });
like image 196
Swapnil Dalvi Avatar answered Oct 23 '22 22:10

Swapnil Dalvi


Consider explicitly setting the header in the $http.post (I put application/json, as I am not sure which of the two versions in your example is the working one, but you can use application/x-www-form-urlencoded if it's the other one):

$http.post("/customer/data/autocomplete", {term: searchString}, {headers: {'Content-Type': 'application/json'} })
        .then(function (response) {
            return response;
        });
like image 21
Adrian B. Avatar answered Oct 23 '22 22:10

Adrian B.


i think the most proper way is to use the same piece of code angular use when doing a "get" request using you $httpParamSerializer will have to inject it to your controller so you can simply do the following without having to use Jquery at all , $http.post(url,$httpParamSerializer({param:val}))

app.controller('ctrl',function($scope,$http,$httpParamSerializer){
  $http.post(url,$httpParamSerializer({param:val,secondParam:secondVal}));
}
like image 27
oneLeggedChicken Avatar answered Oct 23 '22 22:10

oneLeggedChicken