鍍金池/ 問答/HTML5  HTML/ 怎么在 vue-cli里面的config文件夾index.js下面實現(xiàn)跨域請求?

怎么在 vue-cli里面的config文件夾index.js下面實現(xiàn)跨域請求?

1, 看了webpack 的配置說明,但是不知道怎么使用
2,這是我在index里面做的嘗試,

'use strict'

// Template version: 1.2.3
// see http://vuejs-templates.github... for documentation.

const path = require('path')
var express = require('express')
var app = express()
var axios = require('axios')
var apiRoutes = express.Router()

module.exports = {
dev: {

// Paths
assetsSubDirectory: 'static',
assetsPublicPath: '/',
proxyTable: { //跨域。。。代理服務(wù) 
  '/api': {
            target: 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg',
        changeOrigin: true,
        pathRewrite: {
         '^/api': ''
        }
  }
},
// Various Dev Server settings
host: 'localhost', // can be overwritten by process.env.HOST
port: 8085, // can be overwritten by process.env.HOST, if port is in use, a free one will be determined
autoOpenBrowser: true,
errorOverlay: true,
notifyOnErrors: true,
poll: false, // https://webpack.js.org/configuration/dev-server/#devserver-watchoptions-

// Use Eslint Loader?
// If true, your code will be linted during bundling and
// linting errors and warnings will be shown in the console.
useEslint: true,
// If true, eslint errors and warnings will also be shown in the error overlay
// in the browser.
showEslintErrorsInOverlay: false,

/**
 * Source Maps
 */

// https://webpack.js.org/configuration/devtool/#development
devtool: 'eval-source-map',

// If you have problems debugging vue-files in devtools,
// set this to false - it *may* help
// https://vue-loader.vuejs.org/en/options.html#cachebusting
cacheBusting: true,

// CSS Sourcemaps off by default because relative paths are "buggy"
// with this option, according to the CSS-Loader README
// (https://github.com/webpack/css-loader#sourcemaps)
// In our experience, they generally work as expected,
// just be aware of this issue when enabling this option.
cssSourceMap: false,
before(apiRoutes) {
  apiRoutes.get('/lyric', function (req, res) {
      var url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
      axios.get(url, {
        headers: {
          referer: 'https://c.y.qq.com/',
          host: 'c.y.qq.com'
        },
        params: req.query
      }).then((response) => {
        var ret = response.data
        if (typeof ret === 'string') {
            /* \w:字母、數(shù)字、下劃線   中間就是以大括號開始,小括號結(jié)束且不為( 、)的字符,一個和多個*/
          var reg = /^\w+\(({[^()]+})\)$/ /*==> MusicJsonCallback({\"retcode\":0,\"code\":0,\"subcode\...."})*/
          var matches = ret.match(reg)
          if (matches) {
            ret = JSON.parse(matches[1])
          }
        }
        res.json(ret)
      }).catch((e) => {
        console.log(e)
      })
    })
},
after(apiRoutes){
  apiRoutes.get('/lyric', function (req, res) {
      var url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
      axios.get(url, {
        headers: {
          referer: 'https://c.y.qq.com/',
          host: 'c.y.qq.com'
        },
        params: req.query
      }).then((response) => {
        var ret = response.data
        if (typeof ret === 'string') {
            /* \w:字母、數(shù)字、下劃線   中間就是以大括號開始,小括號結(jié)束且不為( 、)的字符,一個和多個*/
          var reg = /^\w+\(({[^()]+})\)$/ /*==> MusicJsonCallback({\"retcode\":0,\"code\":0,\"subcode\...."})*/
          var matches = ret.match(reg)
          if (matches) {
            ret = JSON.parse(matches[1])
          }
        }
        res.json(ret)
        console.log(res.json(ret))
      }).catch((e) => {
        console.log(e)
      })
    })
}

},

3、

export function getLyric(mid) {

  const url = '/api/lyric'

  const data = Object.assign({}, commonParams, {
      g_tk: 5381,
    songmid: mid,
    platform: 'yqq',
    hostUin: 0,
    needNewCode: 0,
    categoryId: 10000000,
    pcachetime: +new Date(),
    format: 'json'
  })

  return axios.get(url, {
    params: data
  }).then((res) => {
    return Promise.resolve(res.data)
  })
}

4、找不到目標(biāo)地址

clipboard.png

clipboard.png

5、 下面的after和before方法,不知道怎么用,起作用的就只是proxy,Please help me! Thanks very much!

回答
編輯回答
撥弦

clipboard.png

clipboard.png

下面加了url:'/api'也是這樣

2017年9月2日 17:21
編輯回答
忘了我

你的proxy里的target和你實際使用的api地址根本對應(yīng)不上啊

2018年4月30日 22:33
編輯回答
鐧簞噯

跨域需要配置proxyTable,你要進行路徑重寫
proxyTable: {
'/api': {
target: 'https://c.y.qq.com',
changeOrigin: true,
pathRewrite: {
'^/api': '/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
}
}
},
url:'/api'

2018年9月7日 20:45