admin管理员组文章数量:1201558
I have a form submit event that calls a WordPress ajax function. The form submit event occurs only once (It is correct as expected). But the ajax function loaded TWICE (expected to occur once).
Here is a sample of my code.
#Jquery
$('#my_form').submit(function () {
console.log("This log print ONCE");
var name = $('#name').val();
var email = $('#email').val();
$.ajax({
type: 'post',
url: '/wp-admin/admin-ajax.php',
data: {
action: 'my_ajax_function',
name: name,
email: email,
},
dataType: "json",
success: function (data) {
$('#response').html(data.message);
}
});
return false; // to prevent default form submit;
});
#WordPress functions.php
<?php
function my_ajax_function() {
custom_log("This log print TWICE");
$name = (isset($_POST["name"])) ? $_POST["name"] : '';
$email = (isset($_POST['email'])) ? $_POST['email'] : '';
// call to a function to encrypt data
encrypt_data($name, $email);
// send response back to ajax
$result['type'] = "success";
$result['message'] = "Email sent successfully";
json_encode($result);
echo $result;
die();
}
add_action('wp_ajax_nopriv_my_ajax_function', 'my_ajax_function');
add_action('wp_ajax_my_ajax_function', 'my_ajax_function');
?>
Ajax functionality is worked. But the "encrypt_data" function called twice. (I have created a log function custom_log in my_ajax_function and it also get printed twice).
Nb: I have checked the Network tab in inspect mode. The my_ajax_function action called only once in network tab.
Why the ajax function loading multiple times (twice) ? Is there anything I am missing?
本文标签: AJAX function running TWICE with WordPress adminajaxphp
版权声明:本文标题:AJAX function running TWICE with WordPress admin-ajax.php 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738614991a2102853.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论