javascript - [Wordpress + PHP + MySQL]插件内的数据库SELECT查询没有返回结果

标签 javascript php jquery mysql wordpress

我正在尝试从插件内的选择查询检索记录,但没有返回任何记录,并且我收到以下错误消息(Web 控制台)。我的问题是,我在这里忽略了什么?:

Object { readyState: 4, getResponseHeader: .ajax/w.getResponseHeader(), getAllResponseHeaders: .ajax/w.getAllResponseHeaders(), setRequestHeader: .ajax/w.setRequestHeader(), overrideMimeType: .ajax/w.overrideMimeType(), statusCode: .ajax/w.statusCode(), abort: .ajax/w.abort(), state: .Deferred/d.state(), always: .Deferred/d.always(), then: .Deferred/d.then(), 12 more… } getregions.js:47:17
error getregions.js:48:17
Internal Server Error getregions.js:49:17

我的php代码如下:

function getregions_scripts() {
  wp_enqueue_script(
    'getregions-script',
    plugin_dir_url(__FILE__) . "assets/getregions.js",
    array('jquery'),
    '1.0',
    true
  );

  wp_localize_script(
    'getregions-script', // this needs to match the name of our enqueued script
    'gymRegions',      // the name of the object
    array('ajaxurl' => admin_url('admin-ajax.php')) // the property/value
  );
}
add_action( 'wp_enqueue_scripts', 'getregions_scripts' );

add_action( 'wp_ajax_showcountries', 'showcountries_callback' );
add_action( 'wp_ajax_no_priv_showcountries', 'showcountries_callback' );

function showcountries_callback() {

  $mydb = new wpdb('user','password','db','localhost');
  $mydb->show_errors();

  $makeselection=$_POST["makeselection"];

  if($makeselection=="getCountryList"){
          //$showcountry = pdo_query("Select distinct wp_usermeta.meta_value, COUNTRY_MAPPING.COUNTRY_NAME from wp_usermeta, COUNTRY_MAPPING where wp_usermeta.meta_key = 'country_registration' and wp_usermeta.meta_value = COUNTRY_MAPPING.COUNTRY_CODE");
     $table_name = $wpdb->prefix . "usermeta";
     $sql = $wpdb->prepare("Select distinct meta_value, COUNTRY_MAPPING.COUNTRY_NAME
                               from {$table_name}, COUNTRY_MAPPING
                               where meta_key = 'country_registration'
                               and meta_value = COUNTRY_MAPPING.COUNTRY_CODE");
     $showcountry = $wpdb->get_results($sql, ARRAY_A);

     if (!$showcountry) {
         //$message  = 'Invalid query: ' . pdo_error() . "\n";
         //$message .= 'Whole query: ' . $showcountry;
         //die($message);
     }else{
         $html = "<option value=''>Select Country</option>";
         foreach($showcountry as $row){
            $html .= "<option value='{$row->meta_value}'>{$row->COUNTRY_NAME}</option>";
         }
         echo json_encode($html, JSON_PRETTY_PRINT);
     }
   }
   wp_die();
 }

我的 javascript (getregions.js) 文件内容如下:

function feedData($) {
    jQuery(document).ready(function ($) {
        var serialized = $('#MyForm').serialize();
        $.ajax({
            cache: false,
            type: "POST",
            async: false,
            url: gymRegions.ajaxurl,
            data:{action: "showcountries", makeselection: "getCountryList", serialized}, 
            dataType: "json",
            success: function (data, status, error) {
                $('#CountryList').append(data);
            },
            error: function (data, status, error) {
                console.log(data);
                console.log(status);
                console.log(error);
            }
        });
    });
}

我的 .htaccess 文件如下:

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

提前致谢。

最佳答案

尝试将 dataType 设置为“html”并删除对 json_encode 的调用 - 只需回显 HTML 即可。您似乎并不想实际接收 JSON 数据。

<小时/>

如果您确实想要接收 JSON 数据,则需要将 HTML 包装到数组中,然后通过 json_encode 运行它:

echo json_encode( array( 'html' => $html ) );

然后在 JavaScript 中,您可以通过对象访问 HTML:

success: function (data, status, error) {
    $('#CountryList').append(data.html);
}

关于javascript - [Wordpress + PHP + MySQL]插件内的数据库SELECT查询没有返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37491966/

相关文章:

php - 在 PHP 中处理 Python 异常/错误

jquery - 如何在浏览器调整大小后居中 JQuery 对话框

jquery - bootstrap 4 文件输入不显示文件名

php - 获取数组的一部分

php - 如何检查 2 列值的总和是否大于数字并更新表

javascript - Vue - 调度完成后调用 store getter?

javascript - AngularJS ng-click : change value of list item that was clicked

javascript - 谷歌地图中工具提示的背景颜色变化

javascript - 如何让网络浏览器自动刷新?

javascript - 使用 jQuery submit() 按下 enter 时提交表单