How to consume this JSON with Javascript?

Asked

Viewed 2,915 times

4

2 answers

4

Because of the CORS restriction, you cannot make a request ajax common.

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

In that case, I’ll illustrate with the JSONP 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 it in the given url and it perfectly accepted the use of JSONP.

  • i do not know, I would put url: https://client-demo-accounts-2.bootstrap.fyre.co/bs3/v3.1/client-demo-accounts-2.fyre.co/379221/MjAxMjcxMjQyOmRlc2lnbmVyLWFwcC0xNDUzODQwMjgxODk0/init ??

  • Yes. The full url you put in place of the url

  • I will not explain what jsonp is, but I will add the reference to the answer. Please consider reading the answers ;). It is important. And don’t forget to vote for the ones you find interesting

  • @Gabrielrodrigues always forgive me with these "fictitious codes", thank you.

  • @Wallacemaxters, if there is no intention to send a callback function on padding, then pq use JSONP instead of JSON?

  • @Tobymosque the intention is to avoid blocking the request that is made for different domains.

  • Anyway, I will keep the answer, as some people still do not know that it is blocked to make ajax requests for different domains, unless it is marked as Access-Control-Allow-Origin on the site where the request is made.

Show 2 more comments

3


Using Javascript only:

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 snippet of Sopt, so you can check the same at Jsfiddle

  • You are making a mistake because it is not from the same origin. That’s why I cited the JSONP issue. I know the AP did not specify the issue of the url being internal or external. It was good that this happened.

  • Wallace, the error occurs due to a choice of OS architecture, if you run the above code in Jsfiddle it will work smoothly.

  • This I understood. But I want to know about the AP. If he does this in his code, it will be the same (the same error gave his code). So I want to know if his domain is marked as Access-Control-Allow-Origin.

  • Friend, how do I put this and a <li> ??

  • @Tobymosque can leave it. Just because he marked his answer, I realized it solved his problem. Hugs :D

  • @Brunobafilli, what do you want to play inside a li? you can add an HTML template that you want to assemble?

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.