小编Adh*_*tal的帖子

如何使用 JavaScript 在 Hackerrank 中发出 AJAX 请求?

我打开 Hackerrank 示例测试并尝试使用可能用于进行 AJAX 调用的方法。XMLHttpReq, fetch, 等等。它们都不起作用;XHRfetch方法不可用。

第一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.fetchglobal.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 …

javascript xmlhttprequest node.js

4
推荐指数
1
解决办法
7971
查看次数

标签 统计

javascript ×1

node.js ×1

xmlhttprequest ×1