我在wordpress中工作,在按下submit按钮时,我希望函数通过ajax返回结果,结果应在屏幕上显示为警报。但当我按submit时,什么都没有显示。
下面是我的代码
Ajax代码(ajaxinsert.js文件)
jQuery(document).ready(function(){
////////////////////////////////////////////////////////////
jQuery("#addimage").submit(function (e) { //form is intercepted
e.preventDefault();
//serialize the form which contains secretcode
var sentdataa = $(this).serializeArray();
//Add the additional param to the data
sentdataa.push({
name: \'action\',
value: \'wp_up\'
})
//set sentdata as the data to be sent
jQuery.post(yess.ajaxurl, sentdataa, function (rez) { //start of funciton
alert(rez);
return false;
} //end of function
,
\'html\'); //set the dataType as json, so you will get the parsed data in the callback
}); // submit end here
});
HTML表单
<form id="addimage" action="" method="post" enctype="multipart/form-data">
<input type="submit" name="upload" style="margin-bottom:15px;">
</form>
PHP代码(我在页面上使用了此代码的快捷码,下面的代码位于functions.PHP文件中):
add_shortcode( \'test\', \'addimage\' );
function wp_up()
{
echo "zeeshanaslamdurrani";
exit();
}
function addimage(){
add_action( \'wp_ajax_wp_up\', \'wp_up\' );
add_action( \'wp_ajax_nopriv_wp_up\', \'wp_up\');
// register & enqueue a javascript file called globals.js
wp_register_script( \'globalss\', get_stylesheet_directory_uri() . "/js/ajaxinsert.js", array( \'jquery\' ) );
wp_enqueue_script( \'globalss\' );
// use wp_localize_script to pass PHP variables into javascript
wp_localize_script( \'globalss\', \'yess\', array( \'ajaxurl\' => admin_url( \'admin-ajax.php\' ) ) );
}