我当时在做一个新闻网站,得到了这个错误。
syntaxError:意外的标记<;在JSON中的位置0
这是一个紧急的项目
null
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();
null
您确定从正确的URL请求吗?
https://cors-anywhere.herokuapp.com/http://newsapi.org/v2/top-headlines?q=india&apiKey=f4176dc9b5194205b4ea64370c715b9b
我看像是两个网址。如果您输入“raw”,就会得到一个html站点,上面写着:缺少所需的请求头。必须指定其中之一: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格式。