Jho*_*val 43 post json http angularjs
我正在使用angularjs处理autocomplete指令但有一些问题.
我有一个具有自动完成输入的表单.当我在那里输入内容时,术语变量将作为JSON发送:

但是,当我使用相同的功能(来自不同的角度控制器,但相同的功能)在另一种形式时,术语变量发送完美,自动完成工作正常:

这是我的角度函数:
$scope.getCustomers = function (searchString) {
return $http.post("/customer/data/autocomplete",
{term: searchString})
.then(function (response) {
return response;
});
};
Run Code Online (Sandbox Code Playgroud)
你觉得怎么了?
Swa*_*lvi 46
使用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.
});
Run Code Online (Sandbox Code Playgroud)
Adr*_* B. 15
考虑在$ http.post中明确设置标题(我把应用程序/ json,因为我不确定您的示例中的哪两个版本是工作的,但您可以使用application/x-www-form-urlencoded if它是另一个):
$http.post("/customer/data/autocomplete", {term: searchString}, {headers: {'Content-Type': 'application/json'} })
.then(function (response) {
return response;
});
Run Code Online (Sandbox Code Playgroud)
小智 12
我认为最合适的方法是在执行"获取"请求时使用相同的代码角度使用,您$httpParamSerializer必须将其注入控制器,这样您就可以完全执行以下操作而无需使用Jquery, $http.post(url,$httpParamSerializer({param:val}))
app.controller('ctrl',function($scope,$http,$httpParamSerializer){
$http.post(url,$httpParamSerializer({param:val,secondParam:secondVal}));
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
161915 次 |
| 最近记录: |