首页 > 解决方案 > VUE - axios Post 不工作

问题描述

我想做可以由 axios 完成的 VUE.JS 的 ajax 调用。我正在从 JS 文件中进行此调用,下面是我迄今为止尝试过的代码。

    <div id="VueCalling">    
        <div class="container">
            <label>Please enter thought </label>
        <input type="text" id="txtThought" class="form-control textboxCustm" v-model="textThought" />
        </div>
        <input type="button" class="btn btn-info" id="btnInsert" value="Insert JS" v-on:click="greet" />
        <br />
        <br />

        <a href="ReadThought.aspx" class="btn btn-primary">Read all thoughts</a>
    </div>
</asp:Content>

这是我的 HTML 代码,现在如下提到的 JS 代码。

new Vue({
    el: '#VueCalling',
    data: function () {
        return {
            textThought: null,
            checkbox: null,
            text: null,
        }
    },
    methods: {
        greet: function (event) {
            // `this` inside methods points to the Vue instance
            var passedEmail = this.textThought;
            // `event` is the native DOM event
            axios.post('Default.aspx/InsertThoughtMethod?Thought="' + passedEmail + '"',
    {
        headers: {
            'Content-type': 'text/xml; charset=\"utf-8\"'
        },
    }, function (data) {
        alert(data);
    }).then(function (response) {
        console.log(response);
    }).catch(function (error) {
        console.log(error);
    });
        }
    }
});

这是我的方法背后的代码:

[WebMethod]
    public static bool InsertThoughtMethod(string Thought)
    {
        return true;
    }

我检查了控制台和网络日志。它给出了这个错误。 网络日志

调试器直到方法才到达。我不能再进一步了。

标签: vue.jsvuejs2axios

解决方案


我处理了这段代码并找到了解决方案。下面的代码运行良好。我检查了。

new Vue({
el: '#VueCalling',
data: function () {
    return {
        textThought: null,
        checkbox: null,
        text: null,
    }
},
methods: {
    greet: function (event) {
        // `this` inside methods points to the Vue instance
        var passedEmail = this.textThought;

        // `event` is the native DOM event
        axios.post('Default.aspx/InsertThoughtMethod', { Thought : passedEmail }).then(function (response) {
    console.log(response);
}).catch(function (error) {
    console.log(error);
});
    }
}
});

推荐阅读