跨域cookies 共享
这是由于,本地调试。涉及到cookies的问题
想要跨域使用的问题
vue 中的mian.js中放入下面代码
import axios from 'axios' axios.defaults.withCredentials = true; //允许axios请求携带cookie等凭证
如果是用JQuery的ajax实现放入以下代码
$.ajax({ url : 'http://127.0.0.1:3000/', type: "GET", crossDomain: true, xhrFields: { withCredentials: true }, success : function(result) { console.log(result) } });
在node服务中插入以下代码
let express = require('express'); let cookieParser=require('cookie-parser') let app = express(); app.use(cookieParser()) app.all('*', function(req, res, next) { res.header("Access-Control-Allow-Origin", req.headers.origin); //需要显示设置来源,不能使用‘*’ res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); res.header("Access-Control-Allow-Credentials",true); //需要加上这个 next(); }); app.get('/', function (req, res) { res.cookie("zhs","666") console.log(req.body) console.log(req.cookies) res.send('12138') }) app.get('/dd', function (req, res) { console.log(req.headers.cookie) res.send('12138+dd') }) app.listen(3000, function () { console.log('server is run...http://127.0.0.1:3000') })
这样就实现的简单啊的 跨域cookies共享~
注意事项:
谷歌浏览器的这个跨域插件千万不能开