简体   繁体   English

Django应用程序中的请求资源上不存在“ Access-Control-Allow-Origin”标头

[英]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.

相关问题 Runkit-请求的资源上不存在“ Access-Control-Allow-Origin”标头 - Runkit - No 'Access-Control-Allow-Origin' header is present on the requested resource 在本地主机上的请求资源上不存在“ Access-Control-Allow-Origin”标头 - No 'Access-Control-Allow-Origin' header is present on the requested resource on localhost No 'Access-Control-Allow-Origin' header is present on the requested resource error - No 'Access-Control-Allow-Origin' header is present on the requested resource error Phonegap请求的资源上没有“Access-Control-Allow-Origin”标头。 因此不允许原点&#39;null&#39;访问 - Phonegap No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access XMLHttpRequest无法加载[archivo]。 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 起源[dominio] - XMLHttpRequest cannot load [archivo]. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin [dominio] 出现错误无法加载资源:请求的资源上不存在“ Access-Control-Allow-Origin”标头 - getting error Failed to load resource: No 'Access-Control-Allow-Origin' header is present on the requested resource XMLHttpRequest无法加载 <instagram OEMBED URL> 。 请求的资源上不存在“ Access-Control-Allow-Origin”标头 - XMLHttpRequest cannot load <instagram OEMBED URL>. No 'Access-Control-Allow-Origin' header is present on the requested resource 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 AngularJS - No 'Access-Control-Allow-Origin' header is present on the requested resource. AngularJS 使用json和jquery的请求资源错误中不存在“ Access-Control-allow-origin”标头 - No 'Access-control-allow-origin' header is present on the requested resource error with json and jquery “向Jawbone UP API发送请求时,”请求的资源上没有&#39;Access-Control-Allow-Origin&#39;标头“错误 - “No 'Access-Control-Allow-Origin' header is present on the requested resource” error when sending request to Jawbone UP API
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM