Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Webpack Vuejs代理无法使用网页包模板_Webpack_Proxy_Vue.js_Cors_Vuejs2 - Fatal编程技术网

Webpack Vuejs代理无法使用网页包模板

Webpack Vuejs代理无法使用网页包模板,webpack,proxy,vue.js,cors,vuejs2,Webpack,Proxy,Vue.js,Cors,Vuejs2,我正在开发一个vuejs项目,在localhost上使用模板,即url:localhost:8080,但我有一个运行在https://foo.bar.com 过去,我一直通过禁用CORS向express服务器发出直接请求,但现在我尝试通过代理发出请求 根据这一点,我在我的config/index.js proxyTable: { // proxy all requests starting with /api to jsonplaceholder '/': { target: 'ht

我正在开发一个vuejs项目,在localhost上使用模板,即url:
localhost:8080
,但我有一个运行在
https://foo.bar.com

过去,我一直通过禁用CORS向express服务器发出直接请求,但现在我尝试通过代理发出请求

根据这一点,我在我的
config/index.js

proxyTable: {
  // proxy all requests starting with /api to jsonplaceholder
  '/': {
  target: 'https://foo.bar.com',
  changeOrigin: true,
  pathRewrite: {
  '^/': ''
  }
}
在我的登录页面
components/login.vue

我有这样的想法:

...
methods: {
  Login(){
     // trying 3 times with different urls
    api.request('post', 'login', {email, password}).then().catch()
    api.request('post', '/login', {email, password}).then().catch()
    api.request('post', 'https://foo.bar.com/login', {email, password}).then().catch()
  }   
}
但我得到一个类似这样的错误:

...
methods: {
  Login(){
     // trying 3 times with different urls
    api.request('post', 'login', {email, password}).then().catch()
    api.request('post', '/login', {email, password}).then().catch()
    api.request('post', 'https://foo.bar.com/login', {email, password}).then().catch()
  }   
}

加载失败https://foo.bar.com/login: 对飞行前请求的响应未通过访问控制检查:请求的资源上不存在“访问控制允许来源”标头。起源'http://localhost:8080因此,不允许访问。

您需要在express应用程序中启用CORS,如下所示

从npm安装CORS包

$ npm install cors
在express应用程序中启用,如下面的代码所示

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

app.use(cors())

app.get('/api/message', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

有关根据需要定制cors软件包的更多信息,请参阅需要在express应用程序中启用cors的部分,如下所示

从npm安装CORS包

$ npm install cors
在express应用程序中启用,如下面的代码所示

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

app.use(cors())

app.get('/api/message', function (req, res, next) {
  res.json({msg: 'This is CORS-enabled for all origins!'})
})

有关根据需要自定义cors软件包的详细信息,请参阅“cors未禁用”。。交叉验证您的代码。显然,它没有被禁用。问题是怎么做。CORS没有被禁用。。交叉验证您的代码。显然,它没有被禁用。问题是怎么做。