[英]Angularjs http post request - params are not reaching to server
I am trying to create post request with angularjs. 我正在尝试使用angularjs创建发布请求。 This is my code for adding an supplier to server.
这是我向服务器添加供应商的代码。
SupplierService.addNewSupplier = function(supplier) {
alert (supplier.name);
alert (supplier.mobile);
var Indata = {'name': supplier.name, 'mobile': supplier.mobile};
//var Indata = {'product': $scope.product, 'product2': $scope.product2 };
var req = {
url: SupplierURL,
method: 'POST',
params: Indata,
headers: {'Content-Type': 'application/json'}
};
//$http.post(SupplierURL, Indata)
$http(req)
.then(function(success){
console.log(success);
//SupplierList.push({id: data, name: supplier.name, mobile: supplier.mobile});
supplier.name = "";
supplier.mobile = "";
},function (error){
console.log(error);
alert ('Supplier add error.');
});
};
After making the request, callback is coming to error part and in console log i can see. 发出请求后,回调将进入错误部分,并且在控制台日志中可以看到。
{error: true, message: "Failed to create supplier. Please try again", name: null, mobile: null} {错误:是,消息:“无法创建供应商。请重试”,名称:null,移动设备:null}
Why name and mobile is not reaching to server. 为什么名称和手机无法到达服务器。 Same request works fine with postman(api testing tool).
邮递员(api测试工具)也可以使用相同的请求。 So i don't thing there can be anu issue in server side code.
所以我不认为服务器端代码中会出现anu问题。 Any help will be appreciated.
任何帮助将不胜感激。
From the $http documentation you can clearly see request parameters: 从$ http文档中,您可以清楚地看到请求参数:
https://docs.angularjs.org/api/ng/service/$http https://docs.angularjs.org/api/ng/service/$http
var req = {
method: 'POST',
url: 'http://example.com',
headers: {
'Content-Type': undefined
},
data: { test: 'test' }
}
$http(req).then(function(){...}, function(){...});
So, in your case you need to switch params
with data
: 因此,在您的情况下,您需要使用
data
切换params
:
var req = {
url: SupplierURL,
method: 'POST',
data: Indata,
headers: {'Content-Type': 'application/json'}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.