我有一张这样的表格:
<form method="post" action="" id="BookingForm">
<label for="email">Email</label>
<input type="text" name="email" id="emailId" value="" />
<input type="hidden" name="action" value="1" />
<input type="submit" name="submitname" value="Send" />
</form>
<div id="container"></div>
我的自定义js文件位于以下路径:assets/js/makebooking。jsMy JS文件makebooking.js :jQuery(document).ready(function(event) {
jQuery(\'#BookingForm\').submit(validateForms);
function validateForms(event) {
event.preventDefault();
var x = MBAjax.ajaxurl;
alert(x);
jQuery.ajax({
action: \'makeBooking\',
type: "POST",
url: MBAjax.admin_url,
success: function(data) {
alert(data);
//jQuery("#container" ).append(data);
},
fail: function(error){
alert("error" + error);
}
});
return false;
}
});
Functions.php 文件:// embed the javascript file that makes the AJAX request
wp_enqueue_script( \'make-booking-ajax\',\'/wp-content/themes/twentyseventeen/assets/js/makebooking.js\', array( \'jquery\' ) );
// declare the URL to the file that handles the AJAX request (wp-admin/admin-ajax.php)
wp_localize_script( \'make-booking-ajax\', \'MBAjax\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
function makeBooking(){
echo "Home again";
return "Home";
}
add_action(\'wp_ajax_make_booking\', \'makeBooking\');
The problem is that the ajax call returns all the dom and not what the php function returns. The code in the php function does not work; the echo message is not printed
<下图显示了问题:谢谢费德里科
<小时>
这是我的状态码和我对ajax的响应