我爱布朗熊 2022-09-16 17:39 采纳率: 79.4%
浏览 170
已结题

在学习vue中代理服务器的时候,出现载入页面时与 ws://192.168.43.89:8080/ws 的连接中断。


const { defineConfig } = require('@vue/cli-service')

module.exports = defineConfig({
  // transpileDependencies: false
  transpileDependencies: true,
  lintOnSave: false, //关闭eslint检查

  // 开启代理服务器
  devServer:{
    port: 8080,
    proxy: 'http://localhost:5000',
    headers:{Connection: "keep-alive"}
  }
})

<template>
    <div >
       <button @click="getStudents">获取学生信息</button>
    </div>
</template>


<script>
   import axios from 'axios'


    export default {
        name:'App',
        methods:{
          getStudents(){
            // axios.get('http://localhost:5000/students').then(
            // 不要再找5000要数据了 我们开启了代理服务器 就找8080就行
            axios.get('http://localhost:8080/students').then(
                // 请求成功时的回调
                response =>{
                    // console.log('请求成功了',response)  如果想要成功后的数据,这样直接写response是不行的
                    // response只是一个响应对象  response.data 才是真正的data数据
                    console.log('请求成功了',response.data)
                },
                // 请求失败时的回调
                error =>{
                    // error.message  失败的原因
                    console.log('请求失败了',error.message)
                }
            )
          }
        }
       
   }
</script>

img

img

  • 写回答

1条回答 默认 最新

  • qq_58910023 2022-09-21 00:46
    关注

    复制粘贴重启即可

    devServer: {
    proxy: { // 代理
    '^/': {
    ws: false ,// 在这里添加本代码,
    target: 'http://localhost:5000',// 连接后端跨域配置跨域
    changeOrigin: true,
    secure: false,
    }}}

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录

报告相同问题?

问题事件

  • 系统已结题 9月29日
  • 已采纳回答 9月21日
  • 创建了问题 9月16日

悬赏问题

  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果