react.js - webpackdevserver跨域请求配置?

查看:720
本文介绍了react.js - webpackdevserver跨域请求配置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

new WebpackDevServer(webpack(config), {

publicPath: config.output.publicPath,
hot: true,
historyApiFallback: true,
stats: { colors: true },
proxy:{
  '/orxxxxxxx': {
    target: 'https://api.xxxxxx/2.0',
    secure: false
  }
}

}).listen(3000, 'localhost', function (err, result) {

if (err) {
    console.log(err);
}
console.log('server start');

});
跨域请求总会报这样的错误
XMLHttpRequest cannot load https://api.xxxxxxxx/2.0/orxx... No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.

求大神帮忙

补充
我的ajax这样写的

$.ajax({

      url:'https://api.xxxxxxx/2.0/orxxxxxxxxx/xxxx',
      type: 'POST',
      dataType: 'json',
      data: values,
      success: function(map){
        var login = map.error;
        if (login='') {
          window.location.href="/dist/about/index.html";
        }
      }
    }) 

解决方案

你的请求没有带上cors的参数
或者后台不允许跨域
反正跟webpack proxy没关系

更新一下,
你jquery ajax跨域没打开

 xhrFields: {
        withCredentials: true
    },

    crossDomain:true,

这篇关于react.js - webpackdevserver跨域请求配置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆