我设计了一个基本的注册表单,我在其中检索基本用户详细信息并尝试通过ajax
将其发送到Wordpress。无论我做什么或尝试什么,我都看到了这个错误:
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'];
}
}
我尝试从调用中删除data
,认为我的数据可能存在问题,但我仍然遇到同样的错误。 PHP中的操作名称为wp_ajax_portal_signup
,因此在jQuery中portal_signup
应该是正确的。