【问题标题】:Ajax Front-end Path in WordpressWordpress 中的 Ajax 前端路径
【发布时间】:2014-11-17 16:37:57
【问题描述】:

我尝试使用 AJAX 为 Wordpress 做插件。

在主文件 (plugin.php) 我添加 ajax.js

函数 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');

我的 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);
     }

在 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;
}

我有错误 404。WP 找不到文件 user.php。如何修复 User.php 的路径?我认为 /wp-content/plugins/mine/uses.php 不正确。

谢谢你的帮助

【问题讨论】:

    标签: javascript php ajax wordpress


    【解决方案1】:

    你有一个印刷错误:

    改变这个:

    ajaxRequest.open("GET", "users.php" + queryString, true);
    

    到这里:

    ajaxRequest.open("GET", "user.php" + queryString, true);
    

    【讨论】:

      猜你喜欢
      • 2014-07-03
      • 2018-01-05
      • 1970-01-01
      • 2019-02-13
      • 1970-01-01
      • 2014-11-09
      • 1970-01-01
      • 2011-09-26
      • 1970-01-01
      相关资源
      最近更新 更多