Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.1k views
in Technique[技术] by (71.8m points)

vue项目,axios请求为localhost开头,vue.config.js配置了无效问题

请求的路径为http://106.14.175.81/oauth/token
vue.config.js配置

module.exports = {
  // local Laravel server address for api proxy
  devServer: {
    proxy: {
      '/api': {
        target: 'http://106.14.175.81',
        changeOrigin: true,
      }
    }
  },
  
  // outputDir should be Laravel public dir
  outputDir: '../../../public/',
  publicPath: '/',

  // for production we use blade template file
  indexPath: process.env.NODE_ENV === 'production'
    ? '../resources/views/icustomer.blade.php'
    : 'index.html',
}

代码是放于php框架里,下面那一部分为配合框架的配置

request.js配置

import axios from 'axios'
const request = axios.create({
  baseURL: '', // 基础路径
  withCredentials: true // 表示请求可以携带cookie
})
export default request

请求配置

import request from '../util/request'
export const getToken = data => {
  return request({
    method: 'post',
    url: 'oauth/token',
    data: data
  })
}

使用

 const arr = {
        grant_type: 'password',
        client_id: '1',
        client_secret: '7VTqL26ZqDLsCuT1PtSiwuol5BrYB5yZ3GCRHwQW',
        username: this.form.username,
        password: this.form.password,
        scope: ''
      }
      const data = await getToken(arr)

报错
image.png
文件路径
image.png

哪位大神能帮我解答下


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

修改两个地方

proxy: {
      '/api': {
        target: 'http://106.14.175.81',
        changeOrigin: true,
        pathRewrite: {'^/api' : ''}
      }
    }

const request = axios.create({
  baseURL: '/api/', // 基础路径
  withCredentials: true // 表示请求可以携带cookie
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...