美文网首页
send corss-domain request with c

send corss-domain request with c

作者: WilliamerTso | 来源:发表于2019-05-10 19:38 被阅读0次

client side


made a request.js file what the 'withCredentials: true,' is most important thing to do. It allows browser to catch cookie with corss-domain request.


import axios from 'axios'

const service = axios.create({
  baseURL: 'http://localhost:3000', // process.env.VUE_APP_BASE_API, // url = base url + request url
  withCredentials: true, // send cookies when cross-domain requests
  timeout: 5000 // request timeout
})

export default service

then you can make a real request in you file.

import request from '@/request'

export function events() {
  return request({
    url: '/api/event',
    method: 'get'
  })
}

server side


first of all you need install cors

var express = require('express')
var cors = require('cors')
var app = express()

app.use(cors({
  credentials: true,
  origin: ['http://localhost:3001'],//client side server address/must be
}));

CAUTION

origin: ['http://localhost:3001']
can not be
origin: '*'
the * will conflict with credentials : true
you have to assign it to your client side address

相关文章

网友评论

      本文标题:send corss-domain request with c

      本文链接:https://www.haomeiwen.com/subject/uqdzoqtx.html