AngularJS至http。以json格式发送数据

时间:2022-08-22 20:18:04

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

我在用angularjs做自动补全指令,但是有一些问题。

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

我有一个具有自动完成输入的表单。当我在那里输入一些东西时,术语变量被发送为JSON:

AngularJS至http。以json格式发送数据

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:

但是,当我用另一种形式使用相同的函数(来自不同的角度控制器,但是相同的函数)时,变量就会完美地发送,而自动补全功能也很好:

AngularJS至http。以json格式发送数据

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?

你认为哪里错了?

3 个解决方案

#1


39  

Use JSON.stringify() to wrap your json

使用json .stringify()包装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.
      });

#2


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}))

我认为最合适的方法是使用相同的代码角使用,当使用$httpParamSerializer进行“get”请求时,必须将它注入到您的控制器中,这样您就可以简单地完成以下操作,而无需使用Jquery。

app.controller('ctrl',function($scope,$http,$httpParamSerializer){
  $http.post(url,$httpParamSerializer({param:val,secondParam:secondVal}));
}

#3


11  

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(我添加了应用程序/json,因为我不确定在您的示例中哪个版本是工作的,但是如果是另一个版本,您可以使用应用程序/x-www-form-urlencode):

$http.post("/customer/data/autocomplete", {term: searchString}, {headers: {'Content-Type': 'application/json'} })
        .then(function (response) {
            return response;
        });

#1


39  

Use JSON.stringify() to wrap your json

使用json .stringify()包装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.
      });

#2


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}))

我认为最合适的方法是使用相同的代码角使用,当使用$httpParamSerializer进行“get”请求时,必须将它注入到您的控制器中,这样您就可以简单地完成以下操作,而无需使用Jquery。

app.controller('ctrl',function($scope,$http,$httpParamSerializer){
  $http.post(url,$httpParamSerializer({param:val,secondParam:secondVal}));
}

#3


11  

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(我添加了应用程序/json,因为我不确定在您的示例中哪个版本是工作的,但是如果是另一个版本,您可以使用应用程序/x-www-form-urlencode):

$http.post("/customer/data/autocomplete", {term: searchString}, {headers: {'Content-Type': 'application/json'} })
        .then(function (response) {
            return response;
        });