nuxt.js 错误:('<', "<!doctype "... is not valid JSON)

问题描述 投票:0回答:1

我是网络新手,我制作了一个带有用于输入名称和密码的表单的页面,但是当我尝试获取数据时,我得到的是 html 页面而不是 json 和错误

'<', "<!doctype "... is not valid JSON
。另外,我的数据应该通过
credentials: 'include'
保存在cookie中,但是这没有发生,我不明白为什么。我正在使用 nuxt.js 并创建一个“spa”项目。如果我现在做错了,请帮助我。

我的页面login.vue脚本:

 <script>
    export default {
       name: 'login',
       data() {
        return {
            name: '',
            password: ''
        }
       },
       
       methods: {
        async submit() {
            const jsn = JSON.stringify({
                name: this.name,
                password: this.password
            });
            const response = await fetch('http://localhost:3000/api/login', {
            method: 'POST',
            headers: {'Accept': 'application/json'},
            credentials: 'include',
            body: jsn
          });

            const data = await response.json();
            console.log(data);
            await this.$router.push('/');
        }
       }
    }
    </script>

我尝试将

headers: {'Content-Type': 'application/json'}
替换为
headers: {'Accept': 'application/json'}
但无助于解决问题。现在我明白发生错误是因为我收到的是 html 页面而不是 json,但现在不知道为什么会发生这种情况。

json post cookies nuxt.js fetch
1个回答
0
投票

我已经找到这个问题的答案了!但现在我觉得真的很愚蠢,所以我使用视频教程来做这个项目,但没有提到我需要一个与 url 请求一起工作的服务器逻辑

http://localhost:3000/api/login
。所以解决方案是创建这个返回 json 答案的后端服务器!

附注也许我可以帮助有同样问题的人)

© www.soinside.com 2019 - 2024. All rights reserved.