奶茶精Gaaa 2024-04-21 19:45 采纳率: 47.4%
浏览 13
已结题

为什么axios接口错误?端口号8090

为什么接口错误?端口号8090

img

img

<script setup>
import { ref } from 'vue'

import {loginAPI} from '@/apis/user'
import 'element-plus/theme-chalk/el-message.css'
import { ElMessage } from 'element-plus'
import {useRouter} from'vue-router'
// import router from '@/router'
// 表单数据对象
const userInfo = ref({
  account: '12056258282',
  password: 'hm#qd@23!',
  port:8090,
  agree: true
})

// 规则数据对象
const rules = {
  account: [
    { required: true, message: '用户名不能为空' }
  ],
  password: [
    { required: true, message: '密码不能为空' },
    { min: 6, max: 14, message: '密码长度要求6-14个字符' }
  ],
  agree: [
    {
      validator: (rule, val, callback) => {
        return val ? callback() : new Error('请先同意协议')
      }
    }
  ]
}

// 3.获取form实例做统一校验
const formRef=ref(null)
const router=useRouter()
const doLogin = () => {
  const { account, password } = userInfo.value
  // 调用实例方法
  formRef.value.validate(async (valid) => {
    // valid: 所有表单都通过校验  才为true
    console.log(valid)
    // 以valid做为判断条件 如果通过校验才执行登录逻辑
    if (valid) {
      //TODO LOGIN
      const res=await loginAPI({account,password})
      console.log(res)
 // TODO LOGIN
//  await loginAPI({ account, password })
      // 1. 提示用户
      ElMessage({ type: 'success', message: '登录成功' })
      // 2. 跳转首页
      router.replace({ path: '/' })


    }
  })
}
</script>


<template>
  <div>
    <header class="login-header">
      <div class="container m-top-20">
        <h1 class="logo">
          <RouterLink to="/">小兔鲜</RouterLink>
        </h1>
        <RouterLink class="entry" to="/">
          进入网站首页
          <i class="iconfont icon-angle-right"></i>
          <i class="iconfont icon-angle-right"></i>
        </RouterLink>
      </div>
    </header>
    <section class="login-section">
      <div class="wrapper">
        <nav>
          <a href="javascript:;">账户登录</a>
        </nav>
        <div class="account-box">
<!-- 
          <div class="form">
            <el-form :model="form" :rules="rules" label-width="60px"
              status-icon>
              <el-form-item prop="account"  label="账户">
                <el-input model-value="form.account"/>
              </el-form-item>
              <el-form-item prop="password " label="密码">
                <el-input model-value="form.password"/>
              </el-form-item>
              <el-form-item label-width="22px">
                <el-checkbox  size="large">
                  我已同意隐私条款和服务条款
                </el-checkbox>
              </el-form-item>
              <el-button size="large" class="subBtn">点击登录</el-button>
            </el-form>
          </div> -->
    <div class="form">
      <el-form ref="formRef" :model="userInfo" :rules="rules" status-icon>
        <el-form-item prop="account" label="账户">
          <el-input v-model="userInfo.account" />
        </el-form-item>
        <el-form-item prop="password" label="密码">
          <el-input v-model="userInfo.password" />
        </el-form-item>
        <el-form-item prop="agree" label-width="22px">
          <el-checkbox v-model="userInfo.agree" size="large">
            我已同意隐私条款和服务条款
          </el-checkbox>
        </el-form-item>
        <el-button size="large" class="subBtn" @click="doLogin">点击登录</el-button>
      </el-form>
    </div>


        </div>
      </div>
    </section>

    <footer class="login-footer">
      <div class="container">
        <p>
          <a href="javascript:;">关于我们</a>
          <a href="javascript:;">帮助中心</a>
          <a href="javascript:;">售后服务</a>
          <a href="javascript:;">配送与验收</a>
          <a href="javascript:;">商务合作</a>
          <a href="javascript:;">搜索推荐</a>
          <a href="javascript:;">友情链接</a>
        </p>
        <p>CopyRight &copy; 小兔鲜儿</p>
      </div>
    </footer>
  </div>
</template>

<style scoped lang='scss'>
.login-header {
  background: #fff;
  border-bottom: 1px solid #e4e4e4;

  .container {
    display: flex;
    align-items: flex-end;
    justify-content: space-between;
  }

  .logo {
    width: 200px;

    a {
      display: block;
      height: 132px;
      width: 100%;
      text-indent: -9999px;
      background: url("@/assets/images/logo.png") no-repeat center 18px / contain;
    }
  }

  .sub {
    flex: 1;
    font-size: 24px;
    font-weight: normal;
    margin-bottom: 38px;
    margin-left: 20px;
    color: #666;
  }

  .entry {
    width: 120px;
    margin-bottom: 38px;
    font-size: 16px;

    i {
      font-size: 14px;
      color: $xtxColor;
      letter-spacing: -5px;
    }
  }
}

.login-section {
  background: url('@/assets/images/login-bg.png') no-repeat center / cover;
  height: 488px;
  position: relative;

  .wrapper {
    width: 380px;
    background: #fff;
    position: absolute;
    left: 50%;
    top: 54px;
    transform: translate3d(100px, 0, 0);
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.15);

    nav {
      font-size: 14px;
      height: 55px;
      margin-bottom: 20px;
      border-bottom: 1px solid #f5f5f5;
      display: flex;
      padding: 0 40px;
      text-align: right;
      align-items: center;

      a {
        flex: 1;
        line-height: 1;
        display: inline-block;
        font-size: 18px;
        position: relative;
        text-align: center;
      }
    }
  }
}

.login-footer {
  padding: 30px 0 50px;
  background: #fff;

  p {
    text-align: center;
    color: #999;
    padding-top: 20px;

    a {
      line-height: 1;
      padding: 0 10px;
      color: #999;
      display: inline-block;

      ~a {
        border-left: 1px solid #ccc;
      }
    }
  }
}

.account-box {
  .toggle {
    padding: 15px 40px;
    text-align: right;

    a {
      color: $xtxColor;

      i {
        font-size: 14px;
      }
    }
  }

  .form {
    padding: 0 20px 20px 20px;

    &-item {
      margin-bottom: 28px;

      .input {
        position: relative;
        height: 36px;

        >i {
          width: 34px;
          height: 34px;
          background: #cfcdcd;
          color: #fff;
          position: absolute;
          left: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 18px;
        }

        input {
          padding-left: 44px;
          border: 1px solid #cfcdcd;
          height: 36px;
          line-height: 36px;
          width: 100%;

          &.error {
            border-color: $priceColor;
          }

          &.active,
          &:focus {
            border-color: $xtxColor;
          }
        }

        .code {
          position: absolute;
          right: 1px;
          top: 1px;
          text-align: center;
          line-height: 34px;
          font-size: 14px;
          background: #f5f5f5;
          color: #666;
          width: 90px;
          height: 34px;
          cursor: pointer;
        }
      }

      >.error {
        position: absolute;
        font-size: 12px;
        line-height: 28px;
        color: $priceColor;

        i {
          font-size: 14px;
          margin-right: 2px;
        }
      }
    }

    .agree {
      a {
        color: #069;
      }
    }

    .btn {
      display: block;
      width: 100%;
      height: 40px;
      color: #fff;
      text-align: center;
      line-height: 40px;
      background: $xtxColor;

      &.disabled {
        background: #cfcdcd;
      }
    }
  }

  .action {
    padding: 20px 40px;
    display: flex;
    justify-content: space-between;
    align-items: center;

    .url {
      a {
        color: #999;
        margin-left: 10px;
      }
    }
  }
}

.subBtn {
  background: $xtxColor;
  width: 100%;
  color: #fff;
}
</style>

import axios from 'axios'

import 'element-plus/theme-chalk/el-message.css'
import { ElMessage } from 'element-plus'

// 创建axios实例
const http = axios.create({
  // baseURL: 'http://pcapi-xiaotuxian-front-devtest.itheima.net',
  ///47.106.220.139/student/login
  baseURL: 'http://47.106.220.139/student/login',

  timeout: 5000
})

// axios请求拦截器
http.interceptors.request.use(config => {
  return config
}, e => Promise.reject(e))

// axios响应式拦截器
http.interceptors.response.use(res => res.data, e => {
  // 统一出错误提示
  ElMessage({
    type:'warning',
    message:e.response.data.message
  })
  return Promise.reject(e)
})


export default http

import http from '@/utils/http'

export const loginAPI =({account,password,port}) =>{
  return http({
    url: '/student/login',
    method:'POST',
    data:{
        account,
        password,
        port
    },
   
  })
}

  • 写回答

5条回答 默认 最新

  • 空白诗 全栈领域优质创作者 2024-04-23 17:06
    关注

    CORS(Cross-Origin Resource Sharing,跨源资源共享)错误是现代Web应用开发中常见的问题,尤其在前后端分离架构下,当一个Web应用尝试从不同于其本身的源(即协议、域名、端口任一项不同的服务器)获取数据时,浏览器基于同源策略(Same-origin policy)会实施限制,从而导致请求失败,并抛出CORS错误。

    在Vue项目中使用axios调用接口遇到CORS错误时,通常有几种解决方法:

    方法一:服务端配置CORS

    最直接的方法是在后端服务器上进行配置,允许特定的前端应用跨域访问资源。你需要在后端服务的响应头中添加如下字段:

    • Access-Control-Allow-Origin: 设置允许跨域访问的来源,可以指定具体的域名或使用*代表所有源(但请注意,对于包含认证信息的请求,通常不应该使用通配符)。
    • Access-Control-Allow-Methods: 允许的HTTP方法列表,比如GET, POST, PUT, DELETE等。
    • Access-Control-Allow-Headers: 如果前端请求包含了非标准的头部信息,需要在这里列出。
    • Access-Control-Allow-Credentials: 设置为true以允许携带Cookie和其他凭据进行跨域请求。

    例如:

    {
      "Access-Control-Allow-Origin": "http://your-front-end-domain.com",
      "Access-Control-Allow-Methods": "GET, POST, PUT, DELETE, OPTIONS",
      "Access-Control-Allow-Headers": "Content-Type, Authorization",
      "Access-Control-Allow-Credentials": "true"
    }
    

    方法二:在Vue项目中配置代理

    如果你无法控制后端服务或者出于开发环境的便捷性考虑,可以在Vue项目中设置代理(proxy),让开发服务器作为中介转发请求到实际后端服务器,避免直接跨域请求。

    对于Vue CLI项目,可以在项目的根目录下创建或修改vue.config.js文件,配置devServer.proxy对象:

    module.exports = {
      devServer: {
        proxy: {
          '/api': {
            target: 'http://your-backend-server.com', // 目标后端服务器地址
            changeOrigin: true, // 是否需要改变原始主机头信息
            pathRewrite: {
              '^/api': '' // 如果后端接口URL不需要/api前缀,可以重写路径
            },
            secure: false, // 如果后端是https且证书不受信任,则设为false
          }
        }
      }
    }
    

    之后,在Vue应用中发起请求时,只需将API的基础路径指向本地代理地址即可:

    import axios from 'axios';
    
    const apiClient = axios.create({
      baseURL: '/api' // 在开发环境下,此请求会被代理至'http://your-backend-server.com'
    });
    
    apiClient.get('/some-endpoint');
    

    注意

    • 生产环境中一般不需要设置代理,因为部署时前后端通常会在同一个域下或者已经由后端处理了CORS问题。
    • 调试生产环境的CORS问题时,确保后端已正确配置并支持跨域请求。
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 5月7日
  • 已采纳回答 4月29日
  • 创建了问题 4月21日

悬赏问题

  • ¥20 有偿:在ubuntu上安装arduino以及其常用库文件。
  • ¥15 请问用arcgis处理一些数据和图形,通常里面有一个根据点划泰森多边形的命令,直接划的弊端是只能执行一个完整的边界,但是我们有时候会用到需要在有很多边界内利用点来执行划泰森多边形的命令
  • ¥30 在wave2foam中执行setWaveField时遇到了如下的浮点异常问题,请问该如何解决呢?
  • ¥20 看图片)删除这个自动化录屏脚本就一直报错找不到脚本文件,如何解决?(相关搜索:bat文件)
  • ¥750 关于一道数论方面的问题,求解答!(关键词-数学方法)
  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件