I have a simple app that is posting an array of json to my API. I have several other posts that are working successfully.
When I investigated I the network traffic I found that the POST was sending an empty array in the payload.
There is data in the variable when the request is made, I have verified this several times.
The Request:
function postDemo(dp) {
var x = dp;
return $q(function (resolve, reject) {
$http({
method: 'POST',
url: BASE + 'acl/newDemo',
data: x,
crossDomain: true,
contentType: 'application/json',
xhrFields: {
withCredentials: true
}
})
.success(function (data, status) {
resolve(function () {
resolve(true);
});
})
.error(function (data, status) {
reject(false);
});
});
}
the var x contains the array and is populated as I stated earlier. The issue seems to be coming from this point. The API is working correctly and is correct in not reading data because none is actually sent.
I have tried changing the content type and that does not make a difference.
The array is from $scope and outputs a complex object that has all values accounted for in the x variable when the POST is made.
This is what the browser is showing as the array value before it is posted:
x = [ billingAddress: Object, billingDetails: Object, company: Object, importCompany: Object, importProfile: Array[0] ]
And a screen shot of the payload:
1) $http already return a promise, no need to use $q her.
2) Use this syntaxe : $http().then(sucess(), error() ) :
https://docs.angularjs.org/api/ng/service/ $http
function postDemo(dp) {
return $http({
method: 'POST',
url: BASE + 'acl/newDemo',
data: dp,
crossDomain: true,
contentType: 'application/json',
xhrFields: {
withCredentials: true
}
}).then(function successCallback(response) {
return true ;
}, function errorCallback(response) {
return false ;
});
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.