![](/img/trans.png)
[英]How to solve Uncaught SyntaxError: Unexpected token < in JSON at position 0 console error?
[英]SyntaxError: Unexpected token < in JSON at position 0 how to solve it
我正在制作一个新闻网站并收到此错误。
SyntaxError: Unexpected token < in JSON at position 0
如何解决?
function getnews(){ fetch('https://cors-anywhere.herokuapp.com/http://newsapi.org/v2/top-headlines?q=india&apiKey=f4176dc9b5194205b4ea64370c715b9b',{headers: new Headers({"X-Requested-With":"hgsofuvuwaoao"})}).then(a => a.json()).then((response) =>{ console.log(response); }) } getnews();
您确定您从正确的 URL 请求?
https://cors-anywhere.herokuapp.com/http://newsapi.org/v2/top-headlines?q=india&apiKey=f4176dc9b5194205b4ea64370c715b9b
对我来说看起来像两个 URL。 如果你输入它“原始”,你会得到一个 html 网站说: Missing required request header. Must specify one of: origin,x-requested-with
Missing required request header. Must specify one of: origin,x-requested-with
。
Json 数据位于
https://newsapi.org/v2/top-headlines?q=india&apiKey=f4176dc9b5194205b4ea64370c715b9b
由于它试图解析 html 它读取 html 标签的<
并在那里引发错误。 尝试更改 URL 并查看是否可以解决问题。
您提供的 URL 的原始数据是
<html><head><link rel="stylesheet" href="resource://content-accessible/plaintext.css"></head><body><pre>Missing required request header. Must specify one of: origin,x-requested-with</pre></body></html>
可以理解,这不是有效的 json 格式。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.