我想使用wordpress ajax调用一个函数,我也为此编写了一段代码。但是,当我调用ajax时,它总是retrun-1。我不知道有谁能帮我解决什么问题。
这是我的密码
作用php
function my_AJAX_processing_function(){
//echo $getLetter = $_GET[\'post_id\'];
print_r($_GET);
exit;
global $wpdb;
global $post;
$result = $wpdb->get_results(\'SELECT * FROM wp_posts where post_title LIKE "\'.$getLetter.\'%" AND post_status = "publish" AND post_type="post"\');
while($row = $result)
{
echo \'<a href="\' . $row["guid"] . \'">\' . $row[\'post_title\'] . \'</a><br>\';
}
die();
}
add_action(\'wp_ajax_nopriv_download_track_resources\', \'my_AJAX_processing_function\');
页脚。php
<script type="text/javascript">
jQuery(function($) {
$(document).ready(function() {
$(".downloadtrack").bind(\'click\', function(){
console.log(this.name);
var siteurl = \'<?php echo esc_url( home_url( \'/\' ) ); ?>\';
alert(siteurl);
var val = this.name;
alert(val);
var data = {action: \'download_track_resources\', post_id: val};
$.ajax({
url:siteurl+\'wp-admin/admin-ajax.php\',
success:function(data){
alert(data);
}
});
});
});
});
</script>
Html代码
<td height="25"><a href="javascript:void(0);" title="Download <?php the_title(); ?>" class="downloadtrack" name="<?php echo $post->ID;?>"><?php the_title(); ?></a></td>
谢谢