AngularJS - $http.post send data as json

后端 未结 3 1791
野的像风
野的像风 2020-12-05 06:19

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

相关标签:
3条回答
  • 2020-12-05 07:10

    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.
          });
    
    0 讨论(0)
  • 2020-12-05 07:12

    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}));
    }
    
    0 讨论(0)
  • 2020-12-05 07:13

    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;
            });
    
    0 讨论(0)
提交回复
热议问题