问题

上一个博客部署好了api之后,前端开始吊发现了跨域的问题。

接口地址

http://111.231.201.164/api/houses  服务器上使用的是nginx转发

数据

前端angular请求

this.http.get('http://111.231.201.164/api/houses').subscribe((res: any) => {
console.log(res);
});

目前测试用的Google 和 Firefox两个。

Google  浏览器

需要注意的事我的Google浏览器已经配置过跨域,就是说服务器的代码无论可不可以跨域我的浏览器都可以访问api。

此时还没在后台或者nginx配置跨域。

Firefox  浏览器

而Firefox还是一如既往跨域问题

 解决过程

1、我先是配置的tornado,虽然没有鸟用

这是网上大部分的教程,都是如此说的,但是却没有起作用。

class BaseHandler(tornado.web.RequestHandler):
# blog.csdn.net/moshowgame 解决跨域问题
def set_default_headers(self):
self.set_header("Access-Control-Allow-Origin", "*") # 这个地方可以写域名
self.set_header("Access-Control-Allow-Headers", "token")
self.set_header('Access-Control-Allow-Methods', 'POST, GET, OPTIONS')
self.set_header('Access-Control-Allow-Credentials', 'true') def write(self, chunk):
self.set_header('Access-Control-Allow-Origin', '*')
super(BaseHandler, self).write(chunk) class MainHandler(BaseHandler):
@decoratore
def get(self):
self.write("Hello, World")
# 访问: http://localhost:8888/story/sishen232 # 显示:U get story id is sishen232 class HouseHandler(BaseHandler):
'''house class''' def __init__(self, application, request):
'''必填参数'''
super().__init__(application, request)
# 预处理
self.data = ByteData(self.request.arguments)
self.params = ['title', 'position', 'size', 'address'] @decoratore
def post(self):
'''提交House接口'''
# # 判断提交参数是否有误
# if(('title' not in raw_data) or ('position' not in raw_data)):
# self.write(json.dumps(
# {"false": {"msg": '参数错误'}}, ensure_ascii=False))
# return
code = paramsCheck(self.data, self.params)
if code == 1:
raw_data = self.data
if('year' not in raw_data):
raw_data['year'] = ''
print(raw_data)
data = self.application.db.execute(
"insert into house(title, position, size, address, year) values('{}', '{}', {}, '{}', '{}')".format(raw_data['title'], raw_data['position'], float(raw_data['size']), raw_data['address'], raw_data['year']))
# self.write(json.dumps({"sum": s}))
self.write(json.dumps(
{"success": {"msg": '添加成功'}}, ensure_ascii=False))
else:
self.write(json.dumps(
{"false": {"msg": '参数错误'}}, ensure_ascii=False)) def get(self, House_id=''):
'''
# 获取House接口
# House_id 存在则获取该条数据,不存在获取所有数据
'''
sql = 'select * from house' if House_id == '' else 'select * from house where id = {id}'.format(
id=House_id)
data = self.application.db.query(sql)
# self.write(json.dumps({"sum": s}))
self.write(json.dumps(
{"success": {"msg": '获取成功', "data": data}}, ensure_ascii=False))

2、nginx配置

方法1没有起作用的话,那多半就是转发出的问题。

location /api/{
allow 111.231.201.164;
proxy_pass_header Server;
proxy_set_header Host $http_host;
proxy_redirect off;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Scheme $scheme;
proxy_pass http://127.0.0.1:8888/;
     ## 跨域
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' "GET, POST, PUT, DELETE, PATCH, OPTIONS";
add_header 'Access-Control-Allow-Headers' "token";
return 200;
}
}

修改之后重启nginx。

Google浏览器

还是一样ok

Firefox 浏览器

也请求到了

到此跨域问题解决。

python3 tornado api + angular8 + nginx 跨域问题的更多相关文章

  1. Nginx跨域及Https配置

    一.跨域 1. 什么是跨域? 跨域:指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器对javascript施加的安全限制(指一个域下的文档或脚本试图去请求另一个域下的资源,这 ...

  2. Nginx跨域了解及模拟和解决

    Nginx跨域 同源策略 何为同源: 1.协议(http/https)相同 2.域名(IP)相同 3.端口相同 详解请看我另一篇文章 https://www.cnblogs.com/you-men/p ...

  3. nginx跨域设置

    nginx跨域问题例子:访问http://10.0.0.10/ 需要能实现跨域 操作:http://10.0.0.10/项目是部署在tomcat里面,tomcat跨域暂时还不会,按照网上的方法操作也没 ...

  4. Api之Cors跨域以及其他跨域方式

    Web Api之Cors跨域以及其他跨域方式(三)   我们知道ajax不能跨域访问,但是有时我们确实需要跨域访问获取数据,所以JSONP就此诞生了,其本质使用的是Script标签,除JSONP以外还 ...

  5. nginx跨域的简单应用

    nginx跨域的简单应用 要求:1.浏览器访问print.qianbaihe.wang/zt 直接调转至 www.flybirdprint.com/zt,浏览器显示域名不变. server { lis ...

  6. Web Api之Cors跨域(干货)---大家一定要看清我写的内容哦

    Web Api之Cors跨域 要想跨域需要准备一下几步骤 1.创建WebAPI(请按照图片先后顺序来) 2.进入NuGet包管理搜 Microsoft.AspNet.WebApi.Cors 进行下载 ...

  7. nginx跨域解决方案

    nginx跨域解决方案Access to Font at 'http://47.104.86.187/yinjiatoupiao2/iconfont/iconfont.woff' from origi ...

  8. nginx 跨域请求访问

    1.nginx跨域请求访问 location ~ .*\.(htm|html)$ { add_header Access-Control-Allow-Origin(请求域名) *(所有域名) http ...

  9. Nginx跨域问题

    Nginx跨域无法访问,通常报错: Failed to load http://172.18.6.30:8086/CityServlet: No 'Access-Control-Allow-Origi ...

随机推荐

  1. react-native中TextInput在ios平台下不能输入中文

    目录 1. github上相关资料 2.需要满足defultValue和value属性 react-native 0.55.4版本,发现TextInput 在iOS平台上无法输入中文的问题. 1. g ...

  2. Docker系列-(3) Docker-compose使用与负载均衡

    上一篇文章介绍了docker镜像的制作与发布,本文主要介绍实际docker工程部署中经常用到的docker-compose工具,以及docker的网络配置和负载均衡. Docker-compose介绍 ...

  3. HtmlAgilityPack 获取节点的子节点

    这个问题真的是好无语 var table = doc.DocumentNode.SelectSingleNode("//table[@class='ddd']"); var a = ...

  4. Android中实现异步轮询上传文件

    前言 前段时间要求项目中需要实现一个刷卡考勤的功能,因为涉及到上传图片文件,为加快考勤的速度,封装了一个异步轮询上传文件的帮助类 效果  先上效果图 设计思路 数据库使用的框架是GreenDao,一个 ...

  5. web开发中浏览器跨域问题

    <system.webServer> <httpProtocol> <customHeaders> <add name="Access-Contro ...

  6. CentOS6.8系统最小化安装

    一.CentOS系统版本 CentOS-6.8-x86_64 二.安装系统 1.打开VMware主页点击创建虚拟机 2.选择镜像 3.启动虚拟机 4.键入回车键直接进行安装 5.按Tab键选择Skip ...

  7. sendRedirect()和forward()方法有什么区别?

    forward是服务器内部的跳转,浏览器的地址栏不会发生变化,同时可以把request和response传递给后一个请求.sendRedirect()是浏览器方面的跳转,要发送两次请求,地址栏也会发生 ...

  8. CCF-CSP题解 201503-4 网络延时

    求树的直径. 两遍\(dfs\)就好了. #include <cstdio> #include <cstring> #include <algorithm> #in ...

  9. contiue和break的用法

    contiue和break的作用分别是什么: 1.continue的意思是终止本次循环,直接进入下一次循环. 注意: continue不能加在循环体的最后一步执行的代码,因为代码加上去毫无意义 2.b ...

  10. javascript基础(001)-js加减乘除注意事项(含类型转换)

    一,加减乘除注意事项: 1.任意类型'+'字符串都被强转字符串 2.数字和布尔类型'+'undefined 结果为 NaN (Not a Number) 3.'-','*','/'操作会尝试把数据转为 ...