[英]AngularJS $http request fails with custom headers
I am trying to add a custom header to my common request like so: 我试图将自定义标头添加到我的常见请求中,如下所示:
myApp.config(['$httpProvider', function ($httpProvider) {
// $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.common['test_key'] = 'kkkkk';
}]);
Without the above header request works fine. 如果没有上述标头请求,效果很好。 but when added gives the following error:
但是添加时会出现以下错误:
{
"data": null,
"status": -1,
"config": {
"method": "GET",
"transformRequest": [
null
],
"transformResponse": [
null
],
"jsonpCallbackParam": "callback",
"url": "http://localhost:8900/tt/",
"data": {},
"headers": {
"Accept": "application/json, text/plain, */*",
"test_key": "kkkkk"
}
},
"statusText": "",
"xhrStatus": "error"
}
On server side I have Django with Django Rest Framework. 在服务器端,我有带有Django Rest Framework的Django。 Server side code is very simple:
服务器端代码非常简单:
from rest_framework.decorators import api_view
from rest_framework.response import Response
@api_view(['GET'])
def test(request):
return Response({'ddddddd'})
urlpatterns = [
url(r'^tt/$', test)
]
What am I doing wrong? 我究竟做错了什么? How should I add these headers?
我应该如何添加这些标题?
Here is how I solved it after looking at the comment by @charlietfl: 这是我查看@charlietfl的评论后解决的方法:
from corsheaders.defaults import default_headers CORS_ALLOW_HEADERS = default_headers + ( 'test_key', )
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.