sách gpt4 ăn đã đi

python - Django 应用程序中的访问控制允许来源

In lại Tác giả: IT Lão Cao 更新时间:2023-10-28 21:41:29 28 4
mua khóa gpt4 giày nike

我正在为基于 Django 的应用程序开发 Phonegap 应用程序,但在尝试进行 Ajax 调用时出现此错误:

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 跨源?

这是我的 Ajax 代码:

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

câu trả lời hay nhất

默认情况下,Django 不提供提供跨域所需的 header 。最简单的方法是使用这个为您处理它的 Django 应用程序:https://github.com/adamchainz/django-cors-headers

  • 添加到已安装的应用中
  • 添加到中间件
  • 然后是……
CORS_ALLOWED_ORIGINS = [
"http://read.only.com",
"http://change.allowed.com",
]

要支持全部允许,只需使用设置...CORS_ALLOW_ALL_ORIGINS = True然后在中间件或 View 中对请求进行任何过滤。

关于python - Django 应用程序中的访问控制允许来源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22355540/

28 4 0
Cựu chiến binh CNTT
Hồ sơ

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá taxi Didi miễn phí
Phiếu giảm giá taxi Didi
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress