下面我有一个应该向服务器发送帖子的函数。
var updateApplicationMetadata = function(appId, editorVersion, previewPubFile){
var deferred = $q.defer();
var result = $http({
method: 'post',
url: '../resource/applications/'+appId,
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
data: {
editorVersion: editorVersion,
previewPubFile: previewPubFile
}
});
result.then(function(data){
deferred.result(data);
console.log('from services: ');
console.log(data);
});
return deferred.promise;
};
我将此函数称为:
$scope.update = function(){
MessageBus.emitMsg('notification','Application updates successful.');
console.log('from update: ');
console.log($scope.application.appId);
console.log($scope.application.editorVersion);
console.log($scope.application.previewPubFile);
DataContext.updateApplicationMetaData($scope.application.appId,$scope.application.editorVersion ,$scope.application.previewPubFile);
};
发送到 updateApplicationMetaData 的所有值都是有效的。我可以使用其余的客户端 POST man,我可以使帖子正常工作。然而,当我以 Angular 来做时,我收到了一个糟糕的请求。 URL 和 header 内容类型是正确的,但我不确定数据对象。一定是畸形的。我在这里缺少什么?
最佳答案
您的代码中有错误。你有:
deferred.result(data);
它应该是:
deferred.resolve(data);
让它工作。此外,您需要传递 'application/json'
作为您的接受类型才能使数据正常工作。
关于javascript - Angular $http 错误请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24439076/