ajax请求真实服务器数据示例

// http://study.163.com/webDev/couresByCategory.htm
// http://study.163.com/webDev/couresByCategory.htm?pageNo=1&psize=20&type=20
// https://xhr.spec.whatwg.org/#states
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest

var xhr = new XMLHttpRequest();
console.log('新建xhr对象,readyState的值:' + xhr.readyState);

xhr.open('GET', 'http://study.163.com/webDev/couresByCategory.htm?pageNo=1&psize=20&type=20');
console.log('使用open方法后,readyState的值:' + xhr.readyState);
xhr.send();

xhr.onreadystatechange = function() {
    if (xhr.readyState === 4 && xhr.status === 200) {
        console.log(xhr.statusText);
        console.log(xhr.responseText);
    }
};
posted @ 2017-08-26 15:21  阿胜4K  阅读(343)  评论(0编辑  收藏  举报