php WordPress管理员 AJAX 400错误,返回0

mu0hgdu0  于 2022-10-30  发布在  PHP
关注(0)|答案(1)|浏览(214)

我已经创建了一个 AJAX 请求发送数据到我的插件,然后将提交一个工作申请。当提交表单时,它返回的页面是空白的,而不是一个0,控制台显示一个400错误admin-ajax.php。

这段代码现在已经根据Plamen的建议进行了更新,我意识到$data变量现在可能没有做任何事情

我的表单的动作如下:

<form method="POST" id="job-application-form" action="<?php echo admin_url( 'admin-ajax.php' ) ?>" class="flex lg:flex-row flex-col flex-wrap">

下面是我的表单页面上的代码:

<script type="text/javascript">
    jQuery(function($) {
        $('#job-application-form').on('submit', function(e) {
            var form = $(this);
            var data = form.serialize();

            $.post(form.attr('action'), data, function(response) {
                console.log(form.attr('action'));
                alert('Got this from the server: ' + response);
            });

            e.preventDefault();

            return false;
        });
    });
</script>

这是我的插件函数的代码:

<?php

    add_action( 'wp_ajax_submit_post', 'submit_post' );
    add_action( 'wp_ajax_nopriv_submit_post', 'submit_post' );

    function submit_post() { 

        $actual_link = parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
        $parts = explode('/', $actual_link['path']);
        $job_id = $parts[count($parts)-1];

        $curl = curl_init();

        $firstName = $_POST['firstName'];
        $lastName = $_POST['lastName'];
        $email = $_POST['email'];
        $phone = $_POST['phone'];
        $postcode = $_POST['postcode'];
        // $info = $_GET['info'];

        $data = array(
            'firstName' => $firstName,
            'lastName'  => $lastName,
            'email'     => $email,
            'phone'     => $phone,
            'address'   => $postcode,
        );
        $payload = json_encode($data);

        var_dump($job_id);
        var_dump($payload);

        $token_url = "/home/.token";
        $file = fopen($token_url, "r") or die("Unable to open job ads file!");
        $access_token_new = fread($file,filesize($token_url));

        curl_setopt_array($curl, array(
            CURLOPT_URL => The API URL . $job_id .'/applications',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => $payload,
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer ' . $access_token_new,
                'Content-Type: application/json',
            ),
        ));

        fclose($file);

        $response = curl_exec($curl);

        curl_close($curl);

        var_dump($response);

        wp_die();
    }

?>

从我所收集的我的 AJAX 请求找不到我的插件功能,但我不确定我是否搞砸了命名或发生了什么事。

ffscu2ro

ffscu2ro1#

您需要以兼容的格式传递HTTP POST请求的有效负载。
最简单的方法是将代码更改为以下代码:
添加名为“action”的隐藏字段,如下所示:

<form method="POST" id="job-application-form" action="<?php echo admin_url( 'admin-ajax.php' ) ?>" class="flex lg:flex-row flex-col flex-wrap">
  ...
  <input type="hidden" name="action" value="submit_post">
</form>

然后像这样更改JavaScript实现,无需使用PHP获取Ajax URL,您可以在<form> action属性中获取它:

jQuery(function($) {
  $('#job-application-form').on('submit', function(e) {
    var form = $(this);
    var data = form.serialize();

    $.post(form.attr('action'), data, function(response) {
       console.log(form.attr('action'));
       alert('Got this from the server: ' + response);
    });

    e.preventDefault();

    return false;
  });
});

相关问题