我在wordpress中遇到ajax提交表单的问题,这是我的代码:
AJAX :
jQuery.ajax({
url: $(this).attr('action'),
type: "POST",
dataType: "JSON",
data: formData,
})
.success(function( response ) {
if( response.success ) {
console.log(response);
} else {
console.log('Error: ' + response.data);
}
})
.error(function( xhr, ajaxOptions, thrownError ) {
console.log('Request failed: ' + thrownError.message);
});
PHP (functions.php):
add_action ('wp_ajax_call_store_voting', 'store_voting') ;
add_action ('wp_ajax_nopriv_store_voting', 'store_voting') ;
function store_voting () {
$return_value = 'Successful!';
echo json_encode($return_value);
exit();
}