gpt4 book ai didi

angularjs - 使用angularjs的补丁请求

转载 作者:行者123 更新时间:2023-12-04 03:07:56 24 4
gpt4 key购买 nike

我正在使用djang-tastypie作为后端和AngularJs作为前端的API。我正在使用angularjs $ http从CRUD发送请求。 GET,POST,PUT一切都很好,但是当我尝试发送PATCH请求时,出现错误,未定义PATCH方法。我已经在angular中创建了一个api调用工厂,但是PATCH请求在那里不起作用。

angular.module('tastypieModule', ['ngResource']).
factory('apiCall', function($http, $resource) {

delete $http.defaults.headers.common['X-Requested-With'];

var apiCall = $resource('/api/v1/:type/:id/',
{type: '@type', username: '@userName', api_key: '@api_key', user: '@userID', id: '@id'},
{
get: {method: 'GET'},
post: {method: 'POST', headers: {'Content-Type': 'application/json'}},
del: {method: 'DELETE', headers: {'Content-Type': 'application/json'}},
update: {method: 'PUT', headers: {'Content-Type': 'application/json'}},
pupdate:{method:'PATCH',headers: {'Content-Type': 'application/json'}}
}
);

return apiCall;
});
function MyCtrl($scope,$resource){
$scope.edit=function(){
id=$scope.E_id
$http.pupdate('/api/v1/quizsetting/'+id+'/', editedquizsetting).
success(function(data, status) {
$scope.status = status;
$scope.data = data;
$scope.editQuizSettingModal = false;
//$scope.quizsettinglist.objects[$scope.e_quizsettingindex]=data;
$(".message").append("object has been created successfully");
})
.
error(function(data, status) {
$scope.data = data || "Request failed";
$scope.status = status;
});
};
}

这是我的HTML代码
<div ng-app="myApp">
<div ng-controller="MyCtrl">
<button type="button" ng-click="edit()">Edit</button>
</div></div>

当我在控制台中使用此代码发送路径请求时,它显示http.patch不是函数。
告诉我如何配置ng-app和服务以使用angularjs发送PATCH请求。

最佳答案

向AngularJS添加PATCH的一个常见问题是,它没有用于该HTTP方法的默认Content-Type header (对于PUT,POST和DELETE,它为application/json; charset = utf-8)。这些是我配置的$ httpProvider添加补丁支持:

module.config(['$httpProvider', function($httpProvider) {
$httpProvider.defaults.headers.patch = {
'Content-Type': 'application/json;charset=utf-8'
}
}])

关于angularjs - 使用angularjs的补丁请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20000176/

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