【发布时间】:2015-06-06 02:45:24
【问题描述】:
我正在开发一个 API,使用 djang-tastypie 作为后端,使用 AngularJs 作为前端。我正在使用 angularjs $http 从 CRUD 发送请求。 GET、POST、PUT 一切都很好,但是当我尝试发送 PATCH 请求时,出现错误 Method PATCH is not defined。我已经创建了一个有角度的 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 angularjs-directive angularjs-scope