Access-Control-Allow-Origin in Django app(Django 应用程序中的访问控制允许来源)
问题描述
我正在为基于 Django 的应用程序开发 Phonegap 应用程序,但在尝试进行 Ajax 调用时出现此错误:
I'm developing a Phonegap app for my Django based app, but when trying to make Ajax calls I get this error:
XMLHttpRequest cannot load http://domain.herokuapp.com/getcsrf/?tags=jquery%2Cjavascript&tagmode=any&format=json. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access.
我怎样才能让我的 Django 应用程序允许某些 url 跨源?
How can I make it so my Django app allows cross origin for some urls?
这是我的 Ajax 代码:
Here's my Ajax code:
get: function() {
$.getJSON("http://domain.herokuapp.com/getcsrf/",
{
tags: "jquery,javascript",
tagmode: "any",
format: "json"
},
function(data) {
$.each(data.items, function(item){
console.log(item);
});
});
}
推荐答案
默认情况下,Django 不提供提供跨域所需的标头.最简单的方法是使用这个为您处理它的 Django 应用程序:https://github.com/adamchainz/django-cors-headers
Django by default does not provide the headers necessary to provide cross origin. The easiest way would be to just use this Django app that handles it for you: https://github.com/adamchainz/django-cors-headers
- 添加到已安装的应用中
- 添加到中间件
- 然后是……
CORS_ALLOWED_ORIGINS = [
"http://read.only.com",
"http://change.allowed.com",
]
要支持全部允许,只需使用设置...CORS_ALLOW_ALL_ORIGINS = True
然后在中间件或视图中对请求进行任何过滤.
to support allowing all, just use the setting...
CORS_ALLOW_ALL_ORIGINS = True
and then do any filtering of the request in middleware or in the view.
这篇关于Django 应用程序中的访问控制允许来源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Django 应用程序中的访问控制允许来源


基础教程推荐
- 为什么我在 Vue.js 中得到 ERR_CONNECTION_TIMED_OUT? 2022-01-01
- 每次设置弹出窗口的焦点 2022-01-01
- 如何在特定日期之前获取消息? 2022-01-01
- Node.js 有没有好的索引/搜索引擎? 2022-01-01
- jQuery File Upload - 如何识别所有文件何时上传 2022-01-01
- WatchKit 支持 html 吗?有没有像 UIWebview 这样的控制器? 2022-01-01
- 如何使用 CSS 显示和隐藏 div? 2022-01-01
- Javascript 在多个元素上单击事件侦听器并获取目标 2022-01-01
- 如何使用sencha Touch2在单页中显示列表和其他标签 2022-01-01
- 什么是不使用 jQuery 的经验技术原因? 2022-01-01