简体   繁体   English

为什么 JSON.parse() 对这个对象不起作用?

[英]Why doesn't JSON.parse() work on this object?

const Http = new XMLHttpRequest(); 
const url='https://www.instagram.com/nasa/?__a=1'; 
Http.open("GET", url); 
Http.send();

Http.onreadystatechange = (e) => {
  console.log(Http.responseText); 
  var instaData = JSON.parse(Http.responseText);
  console.log(instaData); 
}

I'm trying to get a JSON object from an Instagram page so that I can extract some basic user data.我正在尝试从 Instagram 页面获取一个 JSON 对象,以便我可以提取一些基本的用户数据。 The above code gets a string from Instagram that looks like a properly formatted JSON object, but when I try to use JSON.parse on it I get the error message "JSON.parse: unexpected end of data at line 1 column 1 of the JSON data".上面的代码从 Instagram 获取一个看起来像格式正确的 JSON 对象的字符串,但是当我尝试在其上使用 JSON.parse 时,我收到错误消息“JSON.parse:JSON 的第 1 行第 1 列的数据意外结束”数据”。

I can't include the full output of Http.responseText because it's too long at 8,000+ characters, but it starts like this:我无法包含 Http.responseText 的完整输出,因为它在 8,000 多个字符时太长了,但它的开头是这样的:

{"logging_page_id":"profilePage_528817151","show_suggested_profiles":true,"show_follow_dialog":false,"graphql":{"user":{"biography":"Explore the universe and discover our home planet. \ud83c\udf0d\ud83d\ude80\n\u2063\nUncover more info about our images:","blocked_by_viewer":false,"country_block":false,"external_url":"https://www.nasa.gov/instagram","external_url_linkshimmed":"https://l.instagram.com/?u=https%3A%2F%2Fwww.nasa.gov%2Finstagram&e=ATOO8om3o0ed_qw2Ih3Jp_aAPc11qkGuNDxhDV6EOYhKuEK5AGi9-L_yWuJiBASMANV4FrWW","edge_followed_by":{"count":53124504},"followed_by_viewer":false,"edge_follow":

You are trying to do a cross origin request without setting the Origin header.您正在尝试在不设置 Origin 标头的情况下执行跨源请求。 If a given api endpoint supports CORS, then when the Origin header is passed in the request it will reply with the "access-control-allow-origin" header.如果给定的 api 端点支持 CORS,则在请求中传递 Origin 标头时,它将使用“access-control-allow-origin”标头进行回复。

I confirmed that the instagram url in your question does support CORS.我确认您问题中的 instagram 网址确实支持 CORS。

The following code using the fetch api works.以下使用 fetch api 的代码有效。

fetch('https://www.instagram.com/nasa/?__a=1', { mode: 'cors' })
  .then((resp) => resp.json())
  .then((ip) => {
    console.log(ip);
  });

You should read through the MDN CORS info https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS .您应该通读 MDN CORS 信息https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS

Here's also a fixed version of your original code:这也是原始代码的固定版本:

const Http = new XMLHttpRequest();
const url = 'https://www.instagram.com/nasa/?__a=1';

Http.open("GET", url);
Http.setRequestHeader('Origin', 'http://local.geuis.com:2000');
Http.send();

Http.onreadystatechange = (e) => {
  if (Http.readyState === XMLHttpRequest.DONE && Http.status === 200) {
    console.log(Http.responseText);
  }
}

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

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