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

问题描述

我对 AngularJs 完全陌生.我正在尝试使用 Angularjs 向 Django Tastypie API 发送 PATCH 请求.我的代码是

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

但是当我尝试使用此代码发送请求时,我收到一个错误,指出 http.patch 不是一个函数.告诉我如何配置 ng-app 和服务以使用 AngularJs 发送 PATCH 请求.我读到 PATCH 请求在 $resource 中可用,所以我也厌倦了 $resource.但是找到相同的结果.请指导我如何从头开始配置应用程序以发送 CRUD 请求,特别是 PATCH 请求

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

推荐答案

根据您显示的代码,您的错误没有意义,但是将 PATCH 添加到 AngularJS 的一个常见问题是它没有该 HTTP 方法的默认 Content-Type 标头(对于 PUT、POST 和 DELETE,它是 application/json;charset=utf-8).这是我对 $httpProvider 的配置以添加补丁支持:

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'
    }
}])

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

10-15 20:48