我在angular6上有前端(http://localhost:4200),在django1.8上有后端(http://127.0.0.1:8000)。在后端,我的视图如下所示:
def display_form(request):
response = HttpResponse('set csrf cookie for form')
rotate_token(request)
response['Access-Control-Allow-Origin'] = 'http://localhost:4200/login'
response['Access-Control-Allow-Credentials'] = 'true'
response['Access-Control-Allow-Headers'] = 'X-CSRF-TOKEN'
return response
在前端,我的请求函数如下所示:
const xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.open('GET', Config.host + 'users/display_form', true);
xhr.onload = function() {
console.log( 'xhr req ok' + xhr.status + xhr.statusText + xhr.responseText);
}
xhr.onerror = function() {
console.log( 'Ошибка ' + this + xhr.status + xhr.statusText + xhr.responseText);
console.dir(this);
}
xhr.send();
但是在发送xhr请求后,我在chrome网络标签中收到以下错误消息。
我需要成功进行跨域请求,并将cookie设置为浏览器cookie存储。请帮助我。
PS: 如果我使用this chrome扩展名,则请求成功