Javascript $http错误请求
下面我有一个函数,可以向服务器发送帖子Javascript $http错误请求,javascript,ajax,angularjs,Javascript,Ajax,Angularjs,下面我有一个函数,可以向服务器发送帖子 var updateApplicationMetadata = function(appId, editorVersion, previewPubFile){ var deferred = $q.defer(); var result = $http({ method: 'post', url: '../resource/applications/'+appId,
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的所有值均有效。我可以使用rest客户端POST man,我可以让POST正常工作。但是,当我在angular中执行此操作时,我收到了一个错误的请求。URL和标题内容类型是正确的,但我不确定数据对象。它一定是畸形的。我在这里遗漏了什么?您的代码中有一个错误。你有:
deferred.result(data);
它应该是:
deferred.resolve(data);
让它工作。此外,您需要将
'application/json'
作为数据工作的接受类型传递。您的代码中有一个错误。你有:
deferred.result(data);
它应该是:
deferred.resolve(data);
让它工作。此外,您需要将
'application/json'
作为数据工作的接受类型传递。假设您使用的是$q中的$q服务,我看不到结果方法。也许改变:
deferred.result(data);
到
假设您使用$q中的$q服务,我看不到结果方法。也许改变:
deferred.result(data);
到
考虑将ngResource用于rest样式的json请求
考虑将ngResource用于rest样式的json请求