本文介绍了使用 angularjs 的补丁请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 API,使用 djang-tastypie 作为后端,使用 AngularJs 作为前端.我正在使用 angularjs $http 发送来自 CRUD 的请求.GET、POST、PUT 一切正常,但是当我尝试发送 PATCH 请求时出现错误 Method PATCH is not defined.我已经创建了一个 api 调用工厂,但 PATCH 请求在那里不起作用.

angular.module('tastypieModule', ['ngResource']).工厂('apiCall',函数($http,$resource){删除 $http.defaults.headers.common['X-Requested-With'];var apiCall = $resource('/api/v1/:type/:id/',{类型:'@type',用户名:'@userName',api_key:'@api_key',用户:'@userID',id:'@id'},{获取:{方法:'获取'},帖子:{方法:'POST',标题:{'内容类型':'应用程序/json'}},del: {method: 'DELETE', headers: {'Content-Type': 'application/json'}},更新:{方法:'PUT',标题:{'Content-Type':'application/json'}},pupdate:{method:'PATCH',headers: {'Content-Type':'application/json'}}});返回 apiCall;});函数 MyCtrl($scope,$resource){$scope.edit=function(){id=$scope.E_id$http.pupdate('/api/v1/quizsetting/'+id+'/',editedquizsetting).成功(功能(数据,状态){$scope.status = 状态;$scope.data = 数据;$scope.editQuizSettingModal = false;//$scope.quizsettinglist.objects[$scope.e_quizsettingindex]=data;$(".message").append("对象创建成功");}).错误(功能(数据,状态){$scope.data = 数据 ||请求失败";$scope.status = 状态;});};}

这是我的 HTML 代码

<div ng-controller="MyCtrl"><button type="button" ng-click="edit()">编辑</button></div></div>

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

解决方案

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

module.config(['$httpProvider', function($httpProvider) {$httpProvider.defaults.headers.patch = {'内容类型':'应用程序/json;字符集=utf-8'}}])

I am working on an API using djang-tastypie as backend and AngularJs for front end. I am sending request fro CRUD using angularjs $http. GET, POST, PUT everything is fine but when I am trying to send a PATCH request there is error Method PATCH is not defined. I have created a factory of api calls in angular but PATCH request is not working there.

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

this my HTML code

<div ng-app="myApp">
<div ng-controller="MyCtrl">
<button type="button" ng-click="edit()">Edit</button>
</div></div>

when i send a path request using this code in console it shows http.patch is not a function.Tell me how can i configure ng-app and services to send a PATCH request using angularjs.

解决方案

A common issue with adding PATCH to AngularJS is that it doesn't have a default Content-Type header for that HTTP method (which is application/json;charset=utf-8 for PUT, POST and DELETE). And these are my configuration of the $httpProvider to add patch support:

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

这篇关于使用 angularjs 的补丁请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:26