我试图用 ajax 调用 PHP,但它给了我 500,有什么想法吗?
我收到错误代码 500,因为 ajax 正在尝试加载文件!
此代码正在从 Google 统计中检索数据 - 我在我的网站上显示统计信息,如果通过 ajax 按下按钮,则会获取这些统计信息。
PHP 我用 ajax 调用:
add_action('wp_ajax_rs_ajax_statistics', 'rs_ajax_statistics');
function rs_ajax_statistics() {
try {
$optParams = array();
// Required parameter
$metrics = 'ga:uniquePageviews';
$start_date = date('Y-m-d', strtotime('-10 years'));
$end_date = date('Y-m-d');
//Current page path
$path_name = $_SERVER['REQUEST_URI'];
$optParams['filters'] = 'ga:pagePath==' . $path_name;
$result = $analytics->data_ga->get( $analytics_id,
$start_date,
$end_date, $metrics, $optParams);
// Everything is OK
if( $result->getRows() ) {
$views_overall = $result->getRows()[0][0];
echo $views_overall;
}
}
// Something is wrong
catch(Exception $e) {
//echo 'There was an error : - ' . $e->getMessage();
}
}
阿贾克斯调用:
//Statistics
var statistics = $('a.statistics');
var statisticsMessage = $('div.meta-extra');
statistics.click(function() {
console.log('Great!');
jQuery.ajax({
url: ajaxURL,
type: "get",
dataType: "json",
data: {
'action': 'rs_ajax_statistics'
},
success: function(msg) {
statisticsMessage.html(msg);
},
error: function() {
}
});
});