简体   繁体   English

如何显示其他站点的JSON数据

[英]How to Display JSON data from another site

I have a page where is some of JSON data stored like this 我有一个页面,其中存储了一些这样的JSON数据

{
    "video": {
        "user": {
            "first_name": "Curtis",
            "last_name": "Shoch"
        },
        "name": "Darwin Digital",
        "location": "Kennesaw",
        "video_type": "c",
        "created": "2018-01-16T13:11:11.326636Z"
    }
}

I need to take this data and display it on another page, o I tried to do something like this 我需要获取这些数据并将其显示在另一页上,我试图做这样的事情

var getJSON = function(url, callback) {
    var xhr = new XMLHttpRequest();
    xhr.open('get', url, true);
    xhr.responseType = 'json';
    xhr.onload = function() {
      var status = xhr.status;
      if (status == 200) {
        callback(null, xhr.response);
      } else {
        callback(status);
      }
    };
    xhr.send();
};
getJSON('https://dev.truthify.com/api/deeplink/info/bf76b0bd-5210-4c55-af23-c9689ffec536/', function(err, data) {
  if (err != null) {
    alert('Something went wrong: ' + err);
  } else {
    result.innerText = video.name;
  }

But I got a message 但我收到一条消息

No Access-Control-Allow-Origin 没有访问控制允许起源

Header is present on the requested resource because I am using source from another website and it is blocked. 标头出现在所请求的资源上,因为我使用的是来自其他网站的资源,并且该资源已被阻止。 So is there any why to bypass this, or some other way to take this data. 因此,有什么理由可以绕过此方法,或者采用其他方法来获取此数据。

You are getting and json and try to send it in another url in a different origin domain. 您正在获取json,并尝试将其发送到其他原始域中的另一个url中。 In order to access a server on different origin domain u have to make a cors request (Cross-Origin Resource Sharing) . 为了访问不同原始域上的服务器,您必须发出cors请求(跨源资源共享)。 This means you have to set an "Access-Control-Allow-Origin" header in order for your client agent to gain permission on the server. 这意味着您必须设置“ Access-Control-Allow-Origin”标头,以便您的客户端代理获得服务器上的许可。

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

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