Python 烧瓶重定向“;XMLHttpRequest无法加载…“;本地主机错误

Python 烧瓶重定向“;XMLHttpRequest无法加载…“;本地主机错误,python,flask,flask-cors,Python,Flask,Flask Cors,正在本地运行flask,正在尝试调用: @app.route('/foo_route', methods=['POST']) @cross_origin(origin='*') def foo(): return redirect("https://www.google.com/") 我得到以下错误: 无法加载XMLHttpRequest。不 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许使用源“” 通路 我试着使用CORS: a

正在本地运行flask,正在尝试调用:

@app.route('/foo_route', methods=['POST'])
@cross_origin(origin='*')
def foo():
    return redirect("https://www.google.com/")
我得到以下错误:

无法加载XMLHttpRequest。不 “Access Control Allow Origin”标头出现在请求的服务器上 资源。因此,不允许使用源“” 通路

我试着使用CORS:

app = Flask(__name__)
CORS(app)
以及我路线上的@cross_origin()。这里出了什么问题?我读到这可能是chrome在本地运行时的错误?
.

我也有同样的问题!这不是一个Chrome bug,它内置在Chrome中是为了安全。(跨源资源共享)是必须存在于apache
httpd.conf或apache.conf或.htaccess
配置文件中的头文件。如果您使用的是NGINX,则必须编辑
defaults.conf或NGINX.conf文件
,这样web服务器基本上可以接受来自自己域以外其他地方的HTTP请求。修复它的“真正”方法是实际进入web服务器(通过ssh)并编辑适当的.conf以包含此头。如果您在apache上,您将在文件顶部添加
头集访问控制允许源“*”
。完成此操作后,重新启动apache以确保保存更改(
服务httpd restart
)。如果您使用的是NGINX,请使用以下配置:

   #
# Wide-open CORS config for nginx
#
location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}
现在,我想象一下,举个例子,你没有访问web服务器的权限(因为你把google的url放在了url中)。这就是它变得棘手的地方

您的选择之一是使用。它绕过CORS并有效地检索数据。要使用它,您需要运行:

$.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent('http://google.com') + '&callback=?', function(data){
    alert(data.contents);
});
这将检索响应,而与web服务器上存在的COR无关

如果您不想更改任何现有代码,并且正在使用Google Chrome,那么有一种解决CORS问题的方法。您可以做的一件事是安装此浏览器扩展: 你可以绕过CORS运行你的程序


希望这对你有用

我也有同样的问题!这不是一个Chrome bug,它内置在Chrome中是为了安全。(跨源资源共享)是必须存在于apache
httpd.conf或apache.conf或.htaccess
配置文件中的头文件。如果您使用的是NGINX,则必须编辑
defaults.conf或NGINX.conf文件
,这样web服务器基本上可以接受来自自己域以外其他地方的HTTP请求。修复它的“真正”方法是实际进入web服务器(通过ssh)并编辑适当的.conf以包含此头。如果您在apache上,您将在文件顶部添加
头集访问控制允许源“*”
。完成此操作后,重新启动apache以确保保存更改(
服务httpd restart
)。如果您使用的是NGINX,请使用以下配置:

   #
# Wide-open CORS config for nginx
#
location / {
     if ($request_method = 'OPTIONS') {
        add_header 'Access-Control-Allow-Origin' '*';
        #
        # Om nom nom cookies
        #
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        #
        # Custom headers and headers various browsers *should* be OK with but aren't
        #
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
        #
        # Tell client that this pre-flight info is valid for 20 days
        #
        add_header 'Access-Control-Max-Age' 1728000;
        add_header 'Content-Type' 'text/plain charset=UTF-8';
        add_header 'Content-Length' 0;
        return 204;
     }
     if ($request_method = 'POST') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
     if ($request_method = 'GET') {
        add_header 'Access-Control-Allow-Origin' '*';
        add_header 'Access-Control-Allow-Credentials' 'true';
        add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
        add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type';
     }
}
现在,我想象一下,举个例子,你没有访问web服务器的权限(因为你把google的url放在了url中)。这就是它变得棘手的地方

您的选择之一是使用。它绕过CORS并有效地检索数据。要使用它,您需要运行:

$.getJSON('http://whateverorigin.org/get?url=' + encodeURIComponent('http://google.com') + '&callback=?', function(data){
    alert(data.contents);
});
这将检索响应,而与web服务器上存在的COR无关

如果您不想更改任何现有代码,并且正在使用Google Chrome,那么有一种解决CORS问题的方法。您可以做的一件事是安装此浏览器扩展: 你可以绕过CORS运行你的程序


希望这对你有用

我决定将url传递给客户端,然后让客户端重定向

@app.route('/foo_route', methods=['POST'])
@cross_origin(origin='*')
def foo():
    return "https://www.google.com/"
然后在客户端(javascript)上:


我决定将url传递给客户端,然后让客户端重定向

@app.route('/foo_route', methods=['POST'])
@cross_origin(origin='*')
def foo():
    return "https://www.google.com/"
然后在客户端(javascript)上:


谢谢你的回答。进一步研究,我想我将从客户端(javascript)重定向。我不确定这是否是一个好的做法。将服务器url发送到客户端,然后客户端重定向。谢谢您的回答。进一步研究,我想我将从客户端(javascript)重定向。我不确定这是否是一个好的做法。将服务器url发送到客户端,然后客户端重定向。如果发送“https”,则还必须发送身份验证详细信息..而google.com不是身份验证站点。。为什么要使用https如果发送“https”,则还必须发送身份验证详细信息..而google.com不是身份验证网站。。为什么要使用https