获取基本授权

时间:2018-10-20 19:28:35

标签: javascript reactjs fetch

有人可以告诉我此提取语句的语法有什么问题吗?

fetch(url, {
    method: 'GET',
    headers: {
        'Authorization': 'Basic ' + btoa(this.props.api_key + ":" + password),
    },
})

当我不包括标题部分时,会收到401错误,这是预期的,因为此外部API需要API密钥。当我添加标头时,fetch语句似乎甚至没有运行。我在函数的promise中有console.log()条语句,以查看是否收到响应。没有报告错误或任何错误,因此我认为这一定是某种语法错误,正在阻止提取正常运行。

1 个答案:

答案 0 :(得分:0)

Fetch是异步的;它返回一个Promise。您可以使用public class Committer { public string name { get; set; } public string email { get; set; } public DateTime date { get; set; } } public class Tree { public string sha { get; set; } public string url { get; set; } } public class Verification { public bool verified { get; set; } public string reason { get; set; } public string signature { get; set; } public string payload { get; set; } } public class Commit { public Author author { get; set; } public Committer committer { get; set; } public string message { get; set; } public Tree tree { get; set; } public string url { get; set; } public int comment_count { get; set; } public Verification verification { get; set; } } public class Author2 { public string login { get; set; } public int id { get; set; } public string node_id { get; set; } public string avatar_url { get; set; } public string gravatar_id { get; set; } public string url { get; set; } public string html_url { get; set; } public string followers_url { get; set; } public string following_url { get; set; } public string gists_url { get; set; } public string starred_url { get; set; } public string subscriptions_url { get; set; } public string organizations_url { get; set; } public string repos_url { get; set; } public string events_url { get; set; } public string received_events_url { get; set; } public string type { get; set; } public bool site_admin { get; set; } } public class Committer2 { public string login { get; set; } public int id { get; set; } public string node_id { get; set; } public string avatar_url { get; set; } public string gravatar_id { get; set; } public string url { get; set; } public string html_url { get; set; } public string followers_url { get; set; } public string following_url { get; set; } public string gists_url { get; set; } public string starred_url { get; set; } public string subscriptions_url { get; set; } public string organizations_url { get; set; } public string repos_url { get; set; } public string events_url { get; set; } public string received_events_url { get; set; } public string type { get; set; } public bool site_admin { get; set; } } public class Parent { public string sha { get; set; } public string url { get; set; } public string html_url { get; set; } } public class RootObject { public string sha { get; set; } public string node_id { get; set; } public Commit commit { get; set; } public string url { get; set; } public string html_url { get; set; } public string comments_url { get; set; } public Author2 author { get; set; } public Committer2 committer { get; set; } public List<Parent> parents { get; set; } } 在同步样式方法中使用它,或使用Promise链:

await fetch(...)