使用angularjs补丁请求 [英] patch request using angularjs

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

问题描述

我在一个API使用djang-tastypie作为后端和AngularJs的前端工作。我使用angularjs $ HTTP发送请求来回CRUD。 GET,POST,PUT一切都很好,但是当我试图发送一个PATCH请求存在误差法补丁没有定义。我创建了一个工厂API调用的角度,但PATCH请求没有在那里工作。

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

这是我的HTML code

this my HTML code

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

当我发送使用控制台此code通路请求它显示http.patch不是一个函数。
告诉我如何我可以配置N​​G-应用程序和服务的使用angularjs发送PATCH请求。

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.

推荐答案

通过添加补丁来AngularJS一个常见的​​问题是,它并没有为HTTP方法默认的Content-Type头(这是应用程序/ json的;字符集= UTF-8 PUT,POST和DELETE)。而这些都是我的配置$ httpProvider添加补丁支持:

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补丁请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆