sha*_*ebp 2 ajax wordpress firefox
我正在Wordpress / Buddypress中尝试简单的Ajax测试。
下面的代码在IE和Chrome中可以正常运行。但是在Firefox中,会调用javascript,但随后它只会刷新当前页面。在Firebug控制台中没有错误,但响应为空。我不认为该PHP被称为。
要在Firefox中正常运行,我需要更改什么?
javascript:
jQuery(document).ready(function(){
jQuery('#test-form input#save').click(function(){
jQuery('.ajax-loader').toggle();
jQuery('#save').attr({'disabled': true});
jQuery.post( ajaxurl, {
action: 'test_save',
'cookie': encodeURIComponent(document.cookie),
'_wpnonce': jQuery("input#_wpnonce-save").val(),
},
function(response) {
jQuery('.ajax-loader').toggle();
// alerts work up to here in all browsers
jQuery('#test-form').append(response);
});
});
Run Code Online (Sandbox Code Playgroud)
});
的PHP:
add_action( 'bp_after_profile_loop_content', 'show_test', 100 );
add_action('wp_ajax_test_save', 'test_save_function');
function show_test() {
?>
<form action="#" id="test-form">
<?php wp_nonce_field( 'save', '_wpnonce-save' ); ?>
<input type="submit" name="save" id="save" value = "test ajax"/>
<span class="ajax-loader"></span>
</form>
<?php
}
function test_save_function() {
check_ajax_referer('save');
echo "php -button was clicked";
}
Run Code Online (Sandbox Code Playgroud)
小智 5
请注意,只有当您当时登录WordPress时,它才有效!如果您尚未登录,则wp_ajax调用钩子wp_ajax_nopriv_myfunction。
链接:
https://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_nopriv_(action) http://www.simonbattersby.com/blog/2012/06/wordpress-wp_ajax-returning-0-error/
希望这可以节省一个小时左右的时间...