gpt4 book ai didi

javascript - Angular Http put 方法在传递数据时出错(服务器期望表单 param )

转载 作者:行者123 更新时间:2023-11-27 23:04:06 25 4
gpt4 key购买 nike

我正在尝试使用 http put 方法在服务器上传递一些数据,并且服务器正在 put 方法中查找表单参数。

var deferred = $q.defer();
$http({
method: 'PUT',
url: 'http.hello.com/rest/sample/'+fileName,
data: {status: status}
}).success(function(data) {
deferred.resolve(data);
}).error(function() {
deferred.reject(data);
});
return deferred.promise;

但是服务器仍然给出缺失数据自定义错误,那么我如何在 put 方法中传递数据。也尝试 $.param() 但它将数据转换为不正确的字符串

最佳答案

  1. 首先,$http本身返回一个promise,所以你不需要$q
  2. 要以更简洁的方式传递数据,您可以使用以下内容

'use strict';

(function() {

function AppService($http) {

var AppService = {
putStatus(status, filename) {
return $http.put('http.hello.com/rest/sample/'+filename, {status: status})
}
};
return AppService;
}

angular.module('app')
.factory('AppService', AppService);

})();

按如下方式使用(在 Controller 中的某个位置)

AppService.putStatus(status, filename)
.then((response) => {
// do something on success
})
.catch((err) => {
//if error occured
});

关于javascript - Angular Http put 方法在传递数据时出错(服务器期望表单 param ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36759494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com