我打开 Hackerrank 示例测试并尝试使用可能用于进行 AJAX 调用的方法。XMLHttpReq, fetch, 等等。它们都不起作用;XHR和fetch方法不可用。
第一fetch:
async function myFetch() {
let response = await fetch('https://jsonplaceholder.typicode.com/todos/1');
let data = await response.json();
console.log(data);
}
Run Code Online (Sandbox Code Playgroud)
Hackerrank 抛出错误,因为fetch它不是一个函数。我也试过window.fetch,global.fetch无果。
我试过XHR:
function myXHR() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState === 4 && this.status === 200) {
console.log(this.responseText);
// or JSON.parse(this.responseText);
}
};
xmlhttp.open('GET', 'https://jsonplaceholder.typicode.com/todos/1');
xmlhttp.send();
}
Run Code Online (Sandbox Code Playgroud)
Hackerrank 说XMLHttpRequest没有定义。
Hackerrank …