编辑代码

function ajax(URL) {
    return new Promise(function (resolve, reject) {
        var req = new XMLHttpRequest();
        req.open('GET', URL, true);
        req.onload = function () {
            if (req.status === 200) {
                resolve(req.responseText);
            } else {
                reject(new Error(req.statusText));
            }
        };
        req.onerror = function () {
            reject(new Error(req.statusText));
        };
        req.send();
    });
}
var URL = "/try/ajax/testpromise.php";
ajax(URL).then(function onFulfilled(value) {
    console.log('内容是:' + value);
}).catch(function onRejected(error) {
    console.error('错误:' + error);
});