【问题标题】:Vue.JS Axios POST request .then methodVue.JS Axios POST 请求 .then 方法
【发布时间】:2017-11-03 11:49:09
【问题描述】:

我卡住了!

我正在通过 AXIOS 从 VUE 应用程序向服务器发送 POST 请求。 一切正常,服务器响应。 问题是,.then 方法永远不会被调用。怎么可能? 额外的问题你是如何处理 VUE 的请求的?

非常感谢

methods:{
    customMethod1: function() {

        //AXIOS
        var config = {
            headers: {'My Custom Header 1': 'Header-Value'}
        };

        //POST request
        axios.post('http://192.168.56.101:5000/post1', {name: 'Dave'}, config)

        .then(function(response){
            alert("posted successfully");
        });

        },

}

【问题讨论】:

  • 你能显示模块的完整代码吗?也许,你不调用方法
  • 感谢您的回复基里尔。方法customMethod1 被调用。发布请求被执行并且服务器回复。只是.thenblack 我什么都做不了。
  • 尝试添加一个catch 回调,以确保它没有失败。

标签: post vue.js axios


【解决方案1】:

我刚刚找到了解决方案,我不知道是否应该与发布此问题的人分享:他至少可以回答其他试图帮助他的人。 当我在这里找到很多答案时,我发布了我的解决方案(也许不是正确的答案,但它有效)

不是 vue.js 的问题,也不是 axios 的问题,.then 语句只有在服务器端返回时才会调用。

一些代码解释:

Product.update(
        {
            productName: req.body.productName,
            productDesc: req.body.productDesc,
            productOwner: 0,
            productImage: req.body.productImage,
            productState: req.body.ProductState,
            productPrice: req.body.ProductPrice
        },
        {
            where: {
                id: req.body.id
            }
        })
        .then(function (item) {
            console.log("Product update " + item.id);
            res.send({id: item.id});    
        })
        .catch(function (err) {
            console.log("Product update error " + err);
        });

这里重要的一行是:

res.send({id: item.id});  

如果您在服务器端没有返回任何内容,则不会调用客户端的 .then。

让我知道它是否有效,但 +1 我 ;-)

【讨论】:

  • 不幸的是,这是我完成的一个简短测试,我没有设置来测试您的答案。但是,您的回答是有道理的,因为必须有一个返回值才能触发 promise .then。我希望我可以删除这个问题...
【解决方案2】:

您是否通过 Fiddler 之类的工具确认服务器确实发送了响应?可能是服务器从未真正响应。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 2017-06-06
    • 2021-12-12
    • 2017-12-08
    • 2020-09-17
    • 2018-09-15
    • 2021-08-11
    相关资源
    最近更新 更多