這是由於,本地調試。涉及到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共享~
注意事項:
谷歌瀏覽器的這個跨域插件千萬不能開