【问题标题】:Post form data to php script using axios使用 axios 将表单数据发布到 php 脚本
【发布时间】:2020-02-05 14:45:50
【问题描述】:

我正在尝试使用 axios 将表单数据发送到 php 脚本。 axios 的语法是否正确? 如何查看通过 post 方法发送的数据?

我刚开始用 vuetify 和 php 编程,所以我需要一点帮助

methods: {
      formSubmit(e) {
                e.preventDefault();
                let currentObj = this;
                this.axios.post('http://localhost/index.php/',{
                     name : this.name, user : this.username
                })
                .then(function (response) {
                    currentObj.output = response.data;
                })
                .catch(function (error) {
                    currentObj.output = error;
                });
            },
}

在php文件中有:

<?php
require_once 'limonade.php';

$data = $_POST;


dispatch('/api/', 'test1');
function test1()
{
    return 'Hello';
}

run();

【问题讨论】:

    标签: php vue.js vuejs2 http-post vuetify.js


    【解决方案1】:

    您的 php 仅在调用 GET 时起作用,limonade 的 dispatch() 仅用于 GET。

    在您的 php 中,您将在 /api/ url 上创建一个 GET 端点,该端点将执行 test1 函数。这意味着当你通过 get 调用 /api 时,你会得到 Hello 作为答案。

    如果你希望它是 POST(不接触你的 javascript),php 应该是这样的:

     # '/' because you are calling to http://localhost/index.php/ it could be '/whatever' if you call http://localhost/whatever (assuming you have configured everythign as limonade recomends)
    function test2()
    dispatch_post('/', 'test2'); 
    {
        return 'Hello via post';
    }
    

    【讨论】:

    猜你喜欢
    • 2019-04-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-04
    • 1970-01-01
    • 1970-01-01
    • 2013-09-13
    相关资源
    最近更新 更多