Axios Get 请求数据返回“data: ↵ ↵ ↵ ↵"; [英] Axios Get request data comes back with "data: ↵ ↵ ↵ ↵"

查看:26
本文介绍了Axios Get 请求数据返回“data: ↵ ↵ ↵ ↵";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Axios 向 Jobs API 站点发出获取请求.每当我 console.log 响应时,我似乎无法使用它.返回的对象是 {data: "↵ ↵ ↵ ↵"},但看起来像 JSON.

I am using Axios to make a get request to a Jobs API site. Whenever I console.log the response, I can't seem to use it. The object that comes back is {data: "↵ ↵ ↵ ↵"}, but it looks like JSON.

const url = //api string content

axios.get(url)
.then(function(res){
    console.log(res);
})
.catch(function(){
    console.log("err");
})

如果我使用console.log(res.data),则它不会返回以JSON 格式解析.看起来它正在返回一个包含所有数据的函数.它看起来像 displayJobs({"keys": "values"}).我无法获得 res.data.displayJobs.当我直接转到链接时,我的 JSON 查看器会正常解析它.如何让数据以 JSON 格式返回?例如:res.data.jobTitle

If I console.log(res.data), then it doesn't come back parsed in JSON. It looks like it's returning a function with all the data. It looks like displayJobs({"keys": "values"}). I can't get res.data.displayJobs. When I go directly to the link, my JSON viewer parses it normal. How can I get the data to come back as JSON format? ex: res.data.jobTitle

感谢您的帮助.我在其他项目上有一些成功的 API 请求,但我还是个新手,所以我希望这不是一个愚蠢的问题.

Thank you for any help. I've had a few successful API requests on other projects, but I am still fairly new so I hope this isn't a dumb question.

推荐答案

看起来您正在使用 JSONP 端点.您不能将 axios 与 JSONP 端点一起使用.我建议:

It looks like you're working with a JSONP endpoint. You cannot use axios with JSONP endpoints. I'd recommend either:

  1. 为您正在使用的 API 查找替代的非 JSONP 端点,或
  2. 针对该微粒端点尝试使用 jsonp 包.https://github.com/axios/axios/blob/master/COOKBOOK.md#jsonp

如评论中所述,您可以在此处了解有关 JSONP 的更多信息:https://stackoverflow.com/a/3840118/3814251

As noted in the comments, you can learn more about JSONP here: https://stackoverflow.com/a/3840118/3814251

如果您想知道我如何知道如何回答您的问题(因为我发现了解其他人如何找到答案很有帮助),那是因为我将检索到的数据识别为 JSONP 端点通常响应的内容.然后我在 Google 上搜索axios jsonp"以查看 axios 是否与 JSONP 端点一起使用,这让我https://github.com/axios/axios/issues/75(其中指出 axios 不提供 jsonp 支持).我按照那个 GitHub 线程中的链接,然后写了我的答案.

If you're wondering how I knew how to answer your question (as I've found it helpful to learn how other people find answers), it's because I recognized the retrieved data as what JSONP endpoints normally respond with. I then Googled "axios jsonp" to see if axios worked with JSONP endpoints, which let me to https://github.com/axios/axios/issues/75 (where it's noted that axios does NOT offer jsonp be support). I followed the links in that GitHub thread and wrote my answer afterwards.

这篇关于Axios Get 请求数据返回“data: ↵ ↵ ↵ ↵";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆