我正在为GitHub开发Web客户端。我使用native JS
和XMLHttpRequest()
从a demo on regex101.com获取数据。我有一个想法,使进度条指示AJAX响应状态。但我有一个问题:没有Content-Length标头作为响应,并且无法计算加载的数据百分比。
但是当我通过curl发送请求时,Content-Length标头可用。
curl -i https://api.github.com/repos/vmg/redcarpet/issues\?page\=1\&per_page\=3
HTTP/1.1 200 OK
Server: GitHub.com
Date: Wed, 06 Apr 2016 11:28:53 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 8808
Status: 200 OK
X-RateLimit-Limit: 60
X-RateLimit-Remaining: 54
X-RateLimit-Reset: 1459945275
Cache-Control: public, max-age=60, s-maxage=60
...
AJAX请求功能:
var ajaxRequest = function(user, repo) {
var xhttp = new XMLHttpRequest();
var pag = '?page=' + page + '&per_page=' + count;
xhttp.onreadystatechange = function() {
if (xhttp.readyState == 4 && xhttp.status == 200) {
response = JSON.parse(xhttp.responseText);
clearIssues();
addIssues(response);
}
};
xhttp.onprogress = function(event) {
progress_bar.innerHTML = event.loaded + ' loaded';
};
xhttp.open('GET', api_url + 'repos/' + user + '/' + repo + '/issues' + pag, true);
xhttp.setRequestHeader('Accept', 'application/vnd.github.v3+json');
xhttp.setRequestHeader('Content-Type', 'application/json');
xhttp.send();
};
怎么了?