我正在尝试将自定义标头添加到我的公共请求中,如下所示:
myApp.config(['$httpProvider', function ($httpProvider) {
// $httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
$httpProvider.defaults.headers.common['test_key'] = 'kkkkk';
}]);
没有上面的标头请求工作正常。但添加时会出现以下错误:
{
"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"
}
在服务器端,我有Django和Django Rest Framework。服务器端代码非常简单:
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)
]
我做错了什么?我该如何添加这些标题?
答案 0 :(得分:0)
以下是我在@charlietfl:
查看评论后的解决方法from corsheaders.defaults import default_headers CORS_ALLOW_HEADERS = default_headers + ( 'test_key', )