汪的爱琪 2024-03-12 21:21 采纳率: 67.5%
浏览 7
已结题

请求跨域问题导致浏览器拦截

SpringBoot启动服务器端口8080,但是前端访问却是63342

img


前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2.7.14/dist/vue.js"></script>
</head>
<body>
<div id="root">
    当前时间:{{time}}
    <h1>字节跳动</h1>
    <form>
        账户名:<input type="text" name="username" v-model:value="username" required>
        <br>
        密码:<input type="password" name="password" v-model:value="password" required>
        <button type="submit" @click.self.prevent="login">登录</button>
    </form>
    <div @click.stop="showInfo">关于本站</div>
</div>
<script>
    // 创建vue实例
    var vm = new Vue({
        data : {
            username : '',
            password : ''
        },
        methods : {
            login(){
            xhr = new XMLHttpRequest()
            xhr.open('POST', '/user/login', true);
                xhr.setRequestHeader('Content-Type', 'application/json');
                xhr.send(JSON.stringify({
                username : this.username,
                password : this.password
            }))
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    alert('返回的数据'+xhr.response)
                }
            }
            },
            showInfo(){
                alert("同学你好")
            }
        },
        computed : {
            time(){
                localTime = new Date()
                year = localTime.getFullYear()
                month = localTime.getMonth() + 1
                day = localTime.getDate()
                return year + '-' + month + '-' + day
            }
        }

    })
    vm.$mount('#root')
</script>
</body>
</html>


出现的问题

img


就算指定了端口号也不对

img


再次请求

img

  • 写回答

1条回答 默认 最新

  • 酷爱码 2024-03-12 23:36
    关注

    引用jquery.js,使用ajax来代替原来的原生异步请求xhr.open试试

    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 3月13日
  • 创建了问题 3月12日

悬赏问题

  • ¥15 有人会用py或者r画这种图吗
  • ¥15 MOD04_3K图像预处理
  • ¥15 [VASP]关于超胞大小和k 点网格的收敛性测试
  • ¥15 pip下载paddle2onnx离谱错误
  • ¥60 db2move nlzxams import 导出db2备份数据报错
  • ¥15 关于#python#的问题:全文总结功能咨询
  • ¥15 俄罗斯方块中无法同时消除多个满行
  • ¥15 使用gojs3.0,如何在nodeDataArray设置好text的位置,再go.TextBlock alignment中进行相应的改变
  • ¥15 psfusion图像融合指标很低
  • ¥15 银河麒麟linux系统如何修改/etc/hosts权限为777