我在测试 Google Analytics API 时正在加载 javascripts。我的服务器上托管了一些静态 JS 文件(如 googleAnalyticsAuthorization_v3.js 和 googleAnalyticsApi_v3.js),我可以很好地查询/评估它们,但我不知道应该如何查询/评估以下内容: http://apis. google.com/js/client.js?onload=handleClientLoad
我的 AJAX 加载器很简单:
function requestJavascriptWithHttpMethod(filePath, httpMethod)
{
request = new XMLHttpRequest();
request.open(httpMethod, filePath, true);
request.onreadystatechange = function()
{
_log('Request "'+filePath+'": readyState <'+requestReadyStateString(this.readyState)+'>, status <'+requestStatusString(this.status)+'>.');
if (this.readyState == this.DONE &&
requestStatusString(this.status) == 'OK')
{
_log('Loading of "'+filePath+'" finished.');
eval(this.responseText);
}
}
request.send();
_log('Request "'+filePath+'"...');
}
function requestJavascript(filePath)
{ requestJavascriptWithHttpMethod(filePath, "GET"); }
我试图发布 url,但没有执行任何结果(它实际上返回状态码 0)。
requestJavascript('googleAnalyticsAuthorization_v3.js'); //Loads, evaluates well.
requestJavascript('googleAnalyticsApi_v3.js'); //Loads, evaluates well.
requestJavascriptWithHttpMethod('http://apis.google.com/js/client.js?onload=handleClientLoad', "POST"); //Nothing seems happening, returns with status code 0.
它的行为应该与我只是将其包含在客户端 HTML 代码中一样,例如:
<script src="http://apis.google.com/js/client.js?onload=handleClientLoad"></script>
控制台输出为:
Request "googleAnalyticsAuthorization_v3.js"...
Request "googleAnalyticsApi_v3.js"...
Request "http://apis.google.com/js/client.js?onload=handleClientLoad"...
Request "http://apis.google.com/js/client.js?onload=handleClientLoad": readyState <request finished and response is ready>, status <0>.
Request "googleAnalyticsApi_v3.js": readyState <request received>, status <OK>.
Request "googleAnalyticsApi_v3.js": readyState <processing request>, status <OK>.
Request "googleAnalyticsApi_v3.js": readyState <request finished and response is ready>, status <OK>.
Loading of "googleAnalyticsApi_v3.js" finished.
googleAnalytics_v3.js evaluated
Request "googleAnalyticsAuthorization_v3.js": readyState <request received>, status <OK>.
Request "googleAnalyticsAuthorization_v3.js": readyState <processing request>, status <OK>.
Request "googleAnalyticsAuthorization_v3.js": readyState <request finished and response is ready>, status <OK>.
Loading of "googleAnalyticsAuthorization_v3.js" finished.
googleAnalyticsAuthorization_v3.js evaluated
有人可以帮我吗?
其实我只是想把所有的逻辑都封装到javascript中,不依赖HTML端。