nodejs端javascript
const cors = require('cors'); var app=express(); var corsOptions = { origin: 'http://localhost:8080', credentials: true, maxAge: '1728000' //這一項是爲了跨域專門設置的 } app.use(cors(corsOptions))
前端html
1.vue-resource前端
this.$http.get('getlogin',{ credentials: true }).then(res => { console.log(res) })
this.$http.post('postlogin',{userInfo: $('.form-signin').serialize()},{ credentials: true }).then(res => { console.log(res) if(res.body.status != 200) { console.log('登陸失敗') }else { console.log('登陸成功') } })
2.原生js中apivue
var xhr = new XMLHttpRequest(); xhr.open('GET', 'http://www.xxx.com/api'); xhr.withCredentials = true; xhr.onload = onLoadHandler; xhr.send()
3.jquery中java
$.ajax({ url: "http://localhost:8080/orders", type: "GET", xhrFields: { withCredentials: true }, crossDomain: true, success: function (data) { render(data); } });
4.axiosnode
const service = axios.create({ baseURL: process.env.BASE_API, // node環境的不一樣,對應不一樣的baseURL timeout: 5000, // 請求的超時時間 //設置默認請求頭,使post請求發送的是formdata格式數據// axios的header默認的Content-Type好像是'application/json;charset=UTF-8',個人項目都是用json格式傳輸,若是須要更改的話,能夠用這種方式修改 // headers: { // "Content-Type": "application/x-www-form-urlencoded" // }, withCredentials: true // 容許攜帶cookie })
https://www.cnblogs.com/lijinwen/p/8012547.htmljquery