How to consume this JSON with JavaScript?

4

How do I consume this URL using JavaScript?

  

> link

    
asked by anonymous 15.02.2016 / 16:21

2 answers

3

Using just JavaScript:

var url = "https://client-demo-accounts-2.bootstrap.fyre.co/bs3/v3.1/client-demo-accounts-2.fyre.co/379221/MjAxNjAxMjcxMjQyOmRlc2lnbmVyLWFwcC0xNDUzODQwMjgxODk0/init";

var httpRequest = new XMLHttpRequest();
httpRequest.open("GET", url);
httpRequest.responseType = "json";
httpRequest.addEventListener("readystatechange", function () {
  if (httpRequest.readyState == 4){
    if (httpRequest.status == 200){
      console.log(httpRequest.response);
      console.log(httpRequest.response.headDocument);
      console.log(httpRequest.response.headDocument.authors);
      console.log(httpRequest.response.headDocument.content);
    } else {
    
    }
  }
});

httpRequest.send();

The above example will not work on the snippet of SOpt, so you can check the same on JSFiddle a>

    
15.02.2016 / 16:32
4

Because of CORS restriction, you can not make a common ajax pend request.

In this case, in order to circumvent this problem, you can use JSONP .

In this case, I'll illustrate with JSONP of jQuery.

var url = 'https://client-demo-accounts-2.bootstrap.fyre.co/bs3/v3.1/client-demo-accounts-2.fyre.co/379221/MjAxNjAxMjcxMjQyOmRlc2lnbmVyLWFwcC0xNDUzODQwMjgxODk0/init';
$.ajax({
  url: url,
  dataType: "jsonp",
  success: function (response) {
    console.log(response);
  }

});

I tested the url and it fully accepted the use of JSONP .

    
15.02.2016 / 16:23