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

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

问题描述

我正在使用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
  1. Find an alternate, non-JSONP endpoint for the API you are using, or
  2. Try the jsonp package instead for that particulate endpoint. 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请求数据随“数据:数据"一起返回.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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