[英]Ajax Front-end Path in Wordpress
I try to do plugin to Wordpress with AJAX. 我尝试用AJAX做Wordpress的插件。
In main file (plugin.php) I add ajax.js 在主文件(plugin.php)中,添加ajax.js
function add_ajax() {
函数add_ajax(){
wp_register_script( 'custom-script', plugins_url( '/ajax.js', __FILE__ )); wp_register_script( 'custom-script', get_template_directory_uri() . '/ajax.js' ); wp_enqueue_script( 'custom-script' );
}
}
add_action( 'wp_enqueue_scripts', 'add_ajax' );
add_action('wp_enqueue_scripts','add_ajax');
My ajax script look like: 我的ajax脚本如下所示:
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var ajaxDisplay = document.getElementById('ajaxDiv');
ajaxDisplay.innerHTML = ajaxRequest.responseText;
}
}
var kid = document.getElementById('kid').value;
var queryString = "?kid=" + kid;
ajaxRequest.open("GET", "users.php" + queryString, true);
ajaxRequest.send(null);
}
In user.php i have callback 在user.php我有回调
$kid = $_GET['kid'];
if(!empty($kid)) {
$dropdown = "";
for($i=1;$i<=$kid;$i++){
$dropdown .= "<div><label><span>User #".$i."</span><input type=text></label></div>";
}
echo $dropdown;
}
I have Error 404. WP can't find file user.php. 我有错误404。WP无法找到文件user.php。 How can i fix the path to User.php?
如何修复User.php的路径? I thnik, that /wp-content/plugins/mine/uses.php is incorrect.
我想/wp-content/plugins/mine/uses.php不正确。
Thanks for help me 谢谢帮我
You've got a typographical error: 您遇到印刷错误:
Change this: 更改此:
ajaxRequest.open("GET", "users.php" + queryString, true);
To this: 对此:
ajaxRequest.open("GET", "user.php" + queryString, true);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.