美文网首页
web开发-跨域问题

web开发-跨域问题

作者: 前端混合开发 | 来源:发表于2019-05-29 15:17 被阅读0次

    前后端分离容易导致跨域问题,所以前后端都要做相应设置。

    前台

    React 用axios withCredentials做如下设置:

      axios.get(CONFIG.baseUrl + api, { params: data, withCredentials: true })
                .then((res) => {
                    resolve(res)
                })
                .catch((err) => {
                    reject(err)
                })
    

    Angular 同样对 withCredentials做如下设置:

     const clonedRequest = request.clone({
          // headers: auth.getTokenHeader(),
          withCredentials: true,
          url: this.fixUrl(request.url)
        });
    

    后台

    Node.js用cors组件,做如下配置

    const cors = require('cors')
    app.use(cors({credentials: true, origin: true}))
    

    相关文章

      网友评论

          本文标题:web开发-跨域问题

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