简体   繁体   English

SyntaxError: Unexpected token < in JSON at position 0 如何解决

[英]SyntaxError: Unexpected token < in JSON at position 0 how to solve it

i was making a news website and got this error.我正在制作一个新闻网站并收到此错误。

SyntaxError: Unexpected token < in JSON at position 0 SyntaxError: Unexpected token < in JSON at position 0

how to solve it?如何解决?

 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();

Are you sure you are requesting from the correct URL?您确定您从正确的 URL 请求?

https://cors-anywhere.herokuapp.com/http://newsapi.org/v2/top-headlines?q=india&apiKey=f4176dc9b5194205b4ea64370c715b9b

looks like two URLs to me.对我来说看起来像两个 URL。 If you enter it "raw" as you did you get a html site saying: Missing required request header. Must specify one of: origin,x-requested-with如果你输入它“原始”,你会得到一个 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 . Missing required request header. Must specify one of: origin,x-requested-with

The Json data is at Json 数据位于

https://newsapi.org/v2/top-headlines?q=india&apiKey=f4176dc9b5194205b4ea64370c715b9b

Since it is trying to parse html it reads the < of the html tags and throws an error there.由于它试图解析 html 它读取 html 标签的<并在那里引发错误。 Try changing the URL and look if that fixes it.尝试更改 URL 并查看是否可以解决问题。

The raw data at the URL you provide is您提供的 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>

which understandably isn't a valid json format.可以理解,这不是有效的 json 格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM