I'm attempting to to retrieve records from a select query inside a plugin, but no records are being returned, and I am receiving the following error message (web console). My question is, what am I overlooking here?:
我试图从插件中的选择查询中检索记录,但没有返回任何记录,我收到以下错误消息(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
My php code reads as follows:
我的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();
}
My javascript (getregions.js) file reads as follows:
我的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);
}
});
});
}
My .htaccess file reads as:
我的.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
Thanks in advance.
提前致谢。
1 个解决方案
#1
0
Try setting dataType
to "html" and remove your call to json_encode
-- simply echo the HTML out. It doesn't seem like you want to actually receive JSON data.
尝试将dataType设置为“html”并删除对json_encode的调用 - 只需回显HTML即可。您似乎不想实际接收JSON数据。
If you do actually want to receive JSON data, you need to wrap your HTML into an array, then run it through json_encode
:
如果您确实想要接收JSON数据,则需要将HTML包装到数组中,然后通过json_encode运行它:
echo json_encode( array( 'html' => $html ) );
Then in your JavaScript you'd access the HTML through the object:
然后在JavaScript中,您将通过对象访问HTML:
success: function (data, status, error) {
$('#CountryList').append(data.html);
}
#1
0
Try setting dataType
to "html" and remove your call to json_encode
-- simply echo the HTML out. It doesn't seem like you want to actually receive JSON data.
尝试将dataType设置为“html”并删除对json_encode的调用 - 只需回显HTML即可。您似乎不想实际接收JSON数据。
If you do actually want to receive JSON data, you need to wrap your HTML into an array, then run it through json_encode
:
如果您确实想要接收JSON数据,则需要将HTML包装到数组中,然后通过json_encode运行它:
echo json_encode( array( 'html' => $html ) );
Then in your JavaScript you'd access the HTML through the object:
然后在JavaScript中,您将通过对象访问HTML:
success: function (data, status, error) {
$('#CountryList').append(data.html);
}