configure angularjs module to send patch request

寵の児 提交于 2019-11-27 18:09:35

问题


I am totally new to AngularJs. I am trying to send a PATCH request using Angularjs to Django Tastypie API's. My code is

var module = angular.module('myApp', []);

module.config(function ($httpProvider) {
});

function MyController($scope,$http)
{
$scope.patchCall=function(){
    $http({
    url: "/patchrequest/",
    data:data,
    method: "PATCH",
})
.success(function(data){
    console.log("SUCCESS");
    $scope.list = data.items;
}).error(function() {
    console.log("FAIL");
});
}
}

But when I am trying to send a request using this code I am Getting an error that http.patch is not a function. Tell me how can i configure ng-app and services to send a PATCH request using AngularJs. I read PATCH request is available in $resource so i tired it with $resource also. But find the same result. Please guide me how can i configure an app from scratch to send CRUD requests, specially PATCH request


回答1:


Your error doesn't make sense based on the code you're showing, but 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). Here's 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'
    }
}])


来源:https://stackoverflow.com/questions/20305615/configure-angularjs-module-to-send-patch-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!