jQuery和Wordpress ajax调用返回400错误请求

I've designed a basic registration form where I'm retrieving basic user details and trying to send them through ajax to Wordpress. Regardless of what I do or try, I'm seeing this error:

POST http://localhost/career_portal/wp-admin/admin-ajax.php 400 (Bad Request)

jQuery:

jQuery(document).ready(function($) {
$('.submit_signup-form').on('click', function(e) {
    var full_name = $('#signup_full-name').val();
    var username = $('#signup_username').val();
    var email_address = $('#signup_email-address').val();
    var password = $('#signup_password').val();
    var phone = $('#signup_contact').val();
    var cover_letter = $('#signup_cover-letter').val();
    var resume = $('#signup_resume').prop('files')[0];

    var form_data = new FormData();
    form_data.append('action', 'portal_signup');
    form_data.append('name', full_name);
    form_data.append('email', email_address);
    form_data.append('username', username);
    form_data.append('password', password);
    form_data.append('phone', phone);
    form_data.append('cover_letter', cover_letter);
    form_data.append('resume', resume);

    $.ajax({
        type: 'POST',
        url: ajax_url,
        contentType: false,
        processData: false,
        data: form_data,
        success:function(data) {
            console.log(data);
        }
    })
  })
})

PHP:

function career_portal_ajax_signup() {
$user_id = wp_create_user($_POST['username'], $_POST['password'], $_POST['email']);
echo $user_id;
add_user_meta($user_id, "_signup_phone", $_POST['phone']);
add_user_meta($user_id, "_signup_cover_letter", $_POST['cover_letter']);
add_user_meta($user_id, "_signup_name", $_POST['name']);
if(!function_exists('wp_handle_upload')) {
    require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$resume = $_FILES['resume'];
$upload_overrides = array('test_form' => false);
$movefile = wp_handle_upload($resume, $upload_overrides);
if ($movefile && !isset($movefile['error'])) {
     echo "File Upload Successfully";
} else {
    /**
     * Error generated by _wp_handle_upload()
     * @see _wp_handle_upload() in wp-admin/includes/file.php
     */
    echo $movefile['error'];
  }
}

I tried removing data from the call too thinking that there might be a problem with my data but I still get the same error. Action name in PHP is wp_ajax_portal_signup so in jQuery portal_signup should be right I believe.

duangenshi9836
duangenshi9836 哈哈这种情况发生在70%的开发人员第一次使用wordpressajax:)
2 年多之前 回复
dorkahemp972157683
dorkahemp972157683 添加wp_ajax_nopriv_portal_signup完成了这项工作!不敢相信浪费了2天。
2 年多之前 回复
dongyong6045
dongyong6045 另外,在你的ajax函数结束时,请添加“wp_die();”与上面的参考链接示例相同
2 年多之前 回复
du9843
du9843 如果你在没有登录的情况下使用该功能,那么再添加一个具有相同功能的动作wp_ajax_nopriv_portal_signupcodex.wordpress.org/...
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐