【问题标题】:Unable to send get request with headers using axios无法使用 axios 发送带有标头的获取请求
【发布时间】:2017-11-05 11:12:48
【问题描述】:

能够在postman得到回复。但无法进入axios。获取 html 作为响应。会有什么问题?

import axios from 'react-native-axios';

var config = {
  headers: {
    'Content-Type': 'Application/Json',
    'JsonStub-User-Key': '__USER__KEY',
    'JsonStub-Project-Key': '__PROJECT__KEY'
  }
};

export async function menuListByCategories() {
  // simulate an asynchronous operation
  const url = "http://jsonstub.com/burgers";
  axios.get(url, {config})
    .then((response) = > {
      console.log(response.data);
    })
    .
  catch ((error) = > {
    console.log("axios error:", error);
  });
}

更新: check the response of this code

【问题讨论】:

  • 您是否收到类似 404、500 或类似的错误响应?
  • @IzumiSy 得到 200 作为响应。以及它的html格式。不在json中
  • 但是你不能用 axios 得到 200 响应,对吧?那么,你得到了什么?
  • 检查响应的SS。

标签: javascript api react-native axios


【解决方案1】:

您可以在config 中添加data: {},以免Content-Type 被axios 删除。检查下面问题的答案。

Jsonstub response not showing

【讨论】:

  • 感谢您的链接。试过但不工作。我们无法为 GET 请求设置数据,同时,没有数据的请求的“Content-Type”标头会被忽略。我该如何处理?
  • {config} 更改为config 并再试一次怎么样?
  • 是的。如果我更改为 403 状态。
  • 你离你更近了一步。 403 表示“禁止”,因此我认为您的 API 密钥可能无法正确调用 API。你能检查一下吗?
  • 信誉是正确的。我在邮递员中获取 JSON 数据。如果我使用 {config},那么状态是 200 OK 并且它在 html 响应中。我想要它在json中。但无法得到它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-08-13
  • 1970-01-01
  • 2022-01-23
  • 2016-11-25
  • 2018-07-12
  • 2020-02-05
  • 1970-01-01
相关资源
最近更新 更多