[英]No 'Access-Control-Allow-Origin' header is present on the requested resource in Django application
I am developing a django based application where I am using Json call from one application to another,the json call is from HTML file which is in AWS s3 bucket and the json call is from django application.
我正在开发基于django的应用程序,其中我在一个应用程序中使用Json调用到另一个应用程序,json调用来自AWS s3存储桶中的HTML文件,而json调用来自django应用程序。
When I am running/opening the HTML file getting this error 当我运行/打开HTML文件时出现此错误
XMLHttpRequest cannot load https://domain_name/email_view/6/ .
XMLHttpRequest无法加载https:// domain_name / email_view / 6 / 。 No 'Access-Control-Allow-Origin' header is present on the requested resource.
所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin ' https://s3-ap-southeast-1.amazonaws.com ' is therefore not allowed access.
因此,不允许访问源“ https://s3-ap-southeast-1.amazonaws.com ”。 The response had HTTP status code 500.
响应的HTTP状态码为500。
My code to resolve this issue 我的代码来解决这个问题
$(document).ready(function () {
$.get("url", function (data) {
email_otp = JSON.stringify(data);
"some code"
});
});
views.py
views.py
def email_view(request, id):
course = get_object_or_404(CourseWare, pk=id)
user = UserProfile.objects.get(user__id=request.user.id)
myorder = MyOrder.objects.get_or_create(buyer=user, course=course)
if request.is_ajax():
sms_otp = randint(10000, 100000)
return HttpResponse(json.dumps(sms_otp),content_type="application/json")
nginx code
Nginx代码
location /media {
root /opt/pursuite/www;
add_header Access-Control-Allow-Origin "*";
add_header Access-Control-Allow-Methods "GET, OPTIONS";
add_header Access-Control-Allow-Methods "PUT, OPTIONS";
add_header Access-Control-Allow-Methods "GET,POST,OPTIONS,HEA
D";
add_header Access-Control-Allow-Headers "Authorization, Origin,
X-Requested-With, Content-Type, Accept";}
you need to enable cors on https://domain_name/email_view/6/
您需要在
https://domain_name/email_view/6/
上启用cors
you did it there : 你在那里做的:
location /media { ... }
which doesn't seem to be the right location 似乎不是正确的位置
I personnally don't know django but you can follow what's written in this link : 我个人不知道django,但是您可以按照此链接中的内容进行操作 :
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.