【问题标题】:How to use Github API v4 (graphql) with javascript XMLHttpRequest如何将 Github API v4 (graphql) 与 javascript XMLHttpRequest 一起使用
【发布时间】:2020-11-02 00:46:29
【问题描述】:

我正在尝试在 github 上使用 graphql 和 XMLHttpRequest 和 javascript 来获取信息,但是当我尝试我的代码时,什么也没有发生。我已经尝试过文档,但仍然没有得到它。 PS:我只是倾斜并给了我的个人令牌所有访问权限。

const githubURL = "https://api.github.com/graphql";
const token = "MY_PERSONAL_TOKEN";
const submitButton = document.querySelector(".botao");
submitButton.addEventListener("click", githubLoad);
let githubQuery = {
  query: `{
      viewer {
          login
      }
  }`,
};

function githubLoad() {
  let http = new XMLHttpRequest();
  http.open("POST", githubURL, true);
  http.setRequestHeader("Content-type", "application/json");
  http.setRequestHeader("Authorization", "Bearer " + token);
  http.send(JSON.stringify(githubQuery));

  console.log(http.responseText);
}

【问题讨论】:

    标签: javascript api github graphql


    【解决方案1】:

    经过很长时间,我发现 GitHub API V4 仅在我更改此行时才有效:

    http.open("POST", githubURL, true);
    
    to
    
    http.open("POST", githubURL, false);
    

    github 文档中没有关于异步性的信息。刚刚在尝试和错误中发现。

    【讨论】:

      猜你喜欢
      • 2018-03-10
      • 2018-08-25
      • 2018-06-15
      • 2019-04-29
      • 1970-01-01
      • 1970-01-01
      • 2018-01-05
      • 2020-01-12
      相关资源
      最近更新 更多