[英]JSON.parse() Error SyntaxError: Unexpected token < in JSON at position 0
I'm following along this udemy course on fullstack webdev.我正在关注全栈 webdev 上的这个 udemy 课程。 Fun so far until I hit this snag.到目前为止很有趣,直到我遇到了这个障碍。
This lesson is about API and JSON and we are making a "Sign Up" email service using Mailchimp API.本课是关于 API 和 JSON 的,我们正在使用 Mailchimp ZDB9744238D7183ACE16 进行“注册”email 服务
I have the following constant:我有以下常数:
And when using nodemon this is the error I get:当使用 nodemon 这是我得到的错误:
That app.js 43:24 points to the: console.log(JSON.parse(data));
那 app.js 43:24 指向: console.log(JSON.parse(data));
line.线。
I've checked my api key and that's correct, the options
and url
are set correctly.我检查了我的 api 密钥,这是正确的, options
和url
设置正确。
I've been scrathing my head over this one.我一直在为这个问题挠头。
Any ideas where to begin troubleshooting?任何想法从哪里开始故障排除?
Can you do console.log(data)
once and validate that data
is not undefined
.您可以执行一次console.log(data)
并验证data
不是undefined
吗?
If we do JSON.parse(undefined)
, we get the same error as the one you are getting.如果我们执行JSON.parse(undefined)
,我们会得到与您得到的相同的错误。 You can put an if
block to see if data
is not undefined
and then selectively parse.您可以放置一个if
块来查看data
是否undefined
,然后有选择地解析。
if(data != null){
console.log(JSON.Parse(data);
}
Check the data.检查数据。
You don't need of JSON.parse(data) it because the data is already in JSON .您不需要JSON.parse(data)它,因为数据已经在JSON中。
Ok guys, Thank you so much for your support and ideas for troubleshooting.好的,非常感谢您对故障排除的支持和想法。 Here is what I did to troubleshoot:这是我为解决问题所做的:
console.log(response.statusCode);
& console.log(data);
& console.log(data);
from this, I go a status code 404 and a hex for the HTML that Mailchimp uses for the 404 page.由此,我 go 状态码 404 和 Mailchimp 用于 404 页面的 HTML 的十六进制。 I revised the script again and found the issue and corrected it.我再次修改了脚本,发现了问题并更正了。 Now it works as intended.现在它按预期工作。
Edit1: To add to some of the answers in this post, it seems that if you just use console.log(data);
Edit1:要添加到这篇文章中的一些答案,似乎如果你只使用console.log(data);
you end viewing a hex response.您结束查看十六进制响应。 storing the data and using stringify const response= JSON.stringify(data);
存储数据并使用 stringify const response= JSON.stringify(data);
returns decimal value.返回十进制值。 So to console.log the data it does seem that you have to use console.log(JSON.parse(data));
因此,要控制台记录数据,您似乎必须使用console.log(JSON.parse(data));
Thank you all again for your help.再次感谢大家的帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.