ajax出现了一个奇怪的500错误


Getting a weird 500 error with ajax

我试图用ajax调用PHP,但它给了我500,有什么想法吗?

我得到ajax试图加载的文件的错误代码500!

这段代码是从谷歌统计数据中检索数据——我在我的网站上显示统计数据,如果通过ajax按下按钮,就会获取统计数据。

PHP Im使用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();
} 
}

Ajax调用:

    //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() {
                    }
                }); 
        });

记录您在ajax调用中返回的错误

error:function (errorcode, errormsg) {
    console.log (errorcode + ' ' + errormsg);
}

编辑最后的答案是:您应该始终打开错误报告并解决显示的错误。

ini_set('display_errors', 1); 
error_reporting(E_ALL);

使用require时,应使用绝对路径。如果它在您的应用程序根目录中,您可以使用$_SERVER['DOCUMENT_ROOT']来获得您的绝对路径。否则,您可以使用realpath($relative_path),请参阅http://php.net/realpath.