【问题标题】:Post request worked but redirects to error page发布请求有效,但重定向到错误页面
【发布时间】:2019-03-05 20:57:55
【问题描述】:

我在执行 POST 请求时遇到了一个奇怪的问题。我在同一页面上有 3 种不同的带有 post 方法的表单,第一个效果很好。

另外两个似乎有问题:当我点击保存按钮时,它会重定向到带有Cannot POST /http://localhost:4000/cards 的错误页面。页面的 url 是 http://localhost:8080/http://localhost:4000/cards 这是我的本地服务器 url + json 服务器 url。

当我刷新页面时,请求已经生效,因为添加了一张新卡。

这是我的简化代码:

<form action="/http://localhost:4000/teamsettings" method="POST">
  <input name="title" class="input-source" v-model="teamsetting.name" type="text">
  <input name="description" class="input-source" v-model="teamsetting.description" type="text">

 <div type="submit" @click="submitTeamG(teamsetting)">Save</div>
</form>



<form action="/http://localhost:4000/cards" method="POST">
  <input v-model="title">
  <textarea class="input-resume" v-model="description"></textarea>

  <button type="submit" @click="subCard">Save</button>
</form>



<form action="/http://localhost:4000/cards" method="POST">
  <input v-model="card.title">
  <textarea class="input-resume" v-model="card.description"></textarea>

  <button type="submit" @click="modifyCard">Modify</button>
</form>

这是我的 axios 帖子请求:

methods: {
        submitTeamG(teamsetting) {
            axios.put('http://localhost:4000/teamsettings', {
                name: teamsetting.name,
                description: teamsetting.description
            })
                .then(function (response) {
                    console.log(response.data);
                })
                .catch(function (error) {
                    console.log(error);
                });
            window.location="/backoffice";
        },

        subCard() {
            axios.post('http://localhost:4000/cards', {
                title: this.title,
                description: this.description,
            })
                .then(function (response) {
                    console.log(response.data);
                })
                .catch(function (error) {
                    console.log(error);
                });
            window.location="/backoffice";
        },

        modifyCard(card) {
            axios.put('http://localhost:4000/cards', {
                title: card.title,
                description: card.description,
            })
                .then(function (response) {
                    console.log(response.data);
                })
                .catch(function (error) {
                    console.log(error);
                });
            window.location.reload();
        }
    },

我可以在重定向到错误页面之前在控制台中看到 404 错误,但是在数据库 json 文件中添加了新数据。这是什么问题?

感谢您的宝贵时间:)

【问题讨论】:

    标签: javascript json post vue.js axios


    【解决方案1】:

    window.location="/backoffice"; 中使用window.open("/backoffice","_self");

    【讨论】:

    • 感谢您的回复!它并没有改变任何东西
    • 请尝试一次。
    • 我认为问题出在其他地方,因为第一个发布请求工作正常。在另外两个上,在控制台重定向到错误页面之前,我在控制台中有一个错误 404。但是我可以看到数据被添加到db中
    • 您在服务器端有问题。所以请用java服务代码更新问题。以便我们可以纠正问题。
    猜你喜欢
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 2013-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-28
    • 1970-01-01
    相关资源
    最近更新 更多