如何使用PHP从VueJS发布数据?

伊万·伊万诺夫(Ivan Ivanov)

请帮助我,如何使用Vue.js发出的请求发布数据

有的Vue代码

let tests = {
    cat1: {
        name: 'Auth',
        items: {
            authorize2: {
                name: 'Successful',
                subname: '',
                request: {
                    method: 'POST',
                    link: 'auth',
                    data: {
                        login: 'admin',
                        password: 'password'
                    }
                },
                test: (result, status) => {
                    if (status.status == 200) {
                        return true;
                    }

                    return false;
                }
            }}}}

接收此代码的PHP页面在POST数据存储中没有任何内容。

M31

最初,Vue使用vue-resource包通过$ http instance属性执行POST请求:

login(){
    var data = {
      login: 'admin',
      password: 'password'
    }
    this.$http.post('/auth', data)
        .then(response => { /* success callback */ }, response => { /* error callback */ })
}

在Vue2中,vue-resource退休了,他们开始推荐Axios他们还提供了一种覆盖$ http实例属性的方法,使您可以类似的方式调用axios库。

this.$http.post('/auth', data)
    .then(response => { /* success callback */ })
    .catch(error => { /* error callback */ })

或者,您可以只加载Axios软件包并直接调用它:

const axios = require('axios');

...

axios.post('/auth', data)
    .then(response => { /* success callback */ })
    .catch(error => { /* error callback */ })

您甚至可以选择使用普通js或jQuery之类的其他库,并使用$ .ajax()在您的Vue实例中发出请求,但是使用axios是Vue的当前建议。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章