admin管理员组文章数量:1415420
I've never used ajax in wordpress before and I'm trying to make a simple ajax request by following some video tutorials on youtube but it always return this error message: Failed to load resource: the server responded with a status of 400 (Bad Request)
My javascript (inside my own template file):
jQuery.ajax({
url: ajaxurl,
type: "POST",
dataType: 'json',
data: {
action: 'my_action'
},
success: function ( response ) {
console.log("success");
alert("success");
},
error: function( error ){
console.log("error");
alert("error");
}
});
my functions.php file:
<?php
add_action('wp_ajax_my_action', 'insertEmail');
add_action('wp_ajax_nopriv_my_action', 'insertEmail');
function insertEmail(){
echo "<script> alert('functions fil'); </script>"
die();
}
?>
I've never used ajax in wordpress before and I'm trying to make a simple ajax request by following some video tutorials on youtube but it always return this error message: Failed to load resource: the server responded with a status of 400 (Bad Request)
My javascript (inside my own template file):
jQuery.ajax({
url: ajaxurl,
type: "POST",
dataType: 'json',
data: {
action: 'my_action'
},
success: function ( response ) {
console.log("success");
alert("success");
},
error: function( error ){
console.log("error");
alert("error");
}
});
my functions.php file:
<?php
add_action('wp_ajax_my_action', 'insertEmail');
add_action('wp_ajax_nopriv_my_action', 'insertEmail');
function insertEmail(){
echo "<script> alert('functions fil'); </script>"
die();
}
?>
Share
Improve this question
asked Jan 3, 2018 at 15:13
SvanteSvante
1111 silver badge4 bronze badges
7
|
Show 2 more comments
1 Answer
Reset to default 2Here's a base snippet I use for AJAX submits in Wordpress, it might help you out:
<?php
if (isset($_POST['my_theme_ajax_submit']))
if ( wp_verify_nonce( $_POST['nonce'], 'my_theme_ajax_submit' ) )
my_theme_ajax_submit();
function my_theme_ajax_submit() {
// do something
// some php code that fires from AJAX click of #fire
wp_mail( '[email protected]', 'my_theme_ajax_submit() fired', time());
wp_die();
}
?>
<button id='fire'>Fire Something</button>
<script>
jQuery('#fire').click(function(){
jQuery.ajax({
type: 'post',
data: {
"my_theme_ajax_submit": "now",
"nonce" : "<?php echo wp_create_nonce( 'my_theme_ajax_submit' ); ?>"
},
success: function(response) {
jQuery('#fire').text("Somthing Done!");
},
error: function(response) {
jQuery('#fire').text("...error!");
},
});
});
</script>
This can be built better - housing the scripts in wp_enque_script()
, having the my_theme_ajax_submit()
condition check / firing at an early hook like init
(instead of within a template), or using the proper wp_ajax_(action)
hooks (never seem to work for me) - but it should give an idea.
本文标签: phpAjax return code 400
版权声明:本文标题:php - Ajax return code 400 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745204716a2647583.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
<script>
tags don't belong in the output, it expects pure JavaScript – kero Commented Jan 3, 2018 at 16:11ajaxurl
? It is only set by default on admin screens, not the front end. – Milo Commented Jan 3, 2018 at 16:16add_action('wp_ajax_my_action', 'insertEmail');
toadd_action('wp_ajax_my_action', 'my_action');
. That is what codex specifies should be done. Which means changing the handler function name tomy_action
from insertEmail. See: codex.wordpress/AJAX_in_Plugins – Tony M Commented Jan 5, 2018 at 18:02