AngularJS - $http.post 将数据作为 json 发送 [英] AngularJS - $http.post send data as json
本文介绍了AngularJS - $http.post 将数据作为 json 发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用 angularjs 处理自动完成指令,但遇到了一些问题.
I'm working on autocomplete directive with angularjs but having some issues.
我有一个具有自动完成输入的表单.当我在那里输入内容时,term 变量 将作为 JSON 发送:
I have a form which have an autocomplete input. When i type something there, the term variable is sent as 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:
这是我的角度函数:
$scope.getCustomers = function (searchString) {
return $http.post("/customer/data/autocomplete",
{term: searchString})
.then(function (response) {
return response;
});
};
你觉得哪里不对?
推荐答案
使用 JSON.stringify() 来包装你的 json
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.
});
这篇关于AngularJS - $http.post 将数据作为 json 发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文