跨域问题原因是同源策略(协议,域名,端口)必须完全一致
有几种解决跨域问题的方法
- jsonp跨域 - json with padding(填充式json),缺陷是只支持GET请求 ,不是标准技术;
- nginx反向代理 代理服务器 交给后端处理
- 后端修改header 响应头
// app就相当于是服务
app.all("*",function(request,response,next){
response.header("Access-Control-Allow-Origin", "*");//访问控制允许来源:所有
response.header("Access-Control-Allow-Headers", "X-Requested-With");//访问控制允许报头X-Requested-With: xhr请求
response.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");//访问控制允许方法
response.header("X-Powered-By",' 3.2.1');//自定义头信息,表示服务端用3.2.1
response.header("Content-Type", "application/json;charset=utf-8");
next();