[英]Angular $http bad request
在下面,我有一个应该在服务器上发布的函数。
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员,并且可以进行发布。 但是,当我按角度进行操作时,会收到一个错误的请求。 URL和标头内容类型正确,但是我不确定数据对象。 它必须是格式错误的。 我在这里想念什么?
您的代码中有错误。 你有:
deferred.result(data);
它应该是:
deferred.resolve(data);
为它工作。 另外,您需要传递'application/json'
作为接受类型,以便数据正常工作。
假设您正在使用来自https://docs.angularjs.org/api/ng/service/的$ q服务,但没有看到结果方法。 也许改变:
deferred.result(data);
至
deferred.resolve(data);
考虑将ngResource用于其余样式的json请求
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.