简体   繁体   English

Angular $ http错误请求

[英]Angular $http bad request

Below I've got a function that should make a post to the server. 在下面,我有一个应该在服务器上发布的函数。

  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;

        };

I call this function like: 我称这个函数为:

$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);
    };

All of the values sent to the updateApplicationMetaData are valid. 发送到updateApplicationMetaData的所有值均有效。 I can use the rest client POST man and I can make the post work. 我可以使用其余的客户端POST员,并且可以进行发布。 When I do it in angular, however, I get a bad request. 但是,当我按角度进行操作时,会收到一个错误的请求。 The URL and header content type are right, but I'm not sure about the data object. URL和标头内容类型正确,但是我不确定数据对象。 It must be malformed. 它必须是格式错误的。 What am I missing here? 我在这里想念什么?

You've got an error in your code. 您的代码中有错误。 You have: 你有:

deferred.result(data);

and it should be: 它应该是:

deferred.resolve(data);

for it to work. 为它工作。 Also, you need to pass 'application/json' as your accepts type for your data to work. 另外,您需要传递'application/json'作为接受类型,以便数据正常工作。

Assuming you are using the $q service from https://docs.angularjs.org/api/ng/service/ $q I don't see a result method. 假设您正在使用来自https://docs.angularjs.org/api/ng/service/的$ q服务,但没有看到结果方法。 Perhaps change: 也许改变:

deferred.result(data);

To

deferred.resolve(data);

consider using ngResource for rest style json requests 考虑将ngResource用于其余样式的json请求

Angular resource docs Angular资源文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM