How do I view the JSON of the API?

Asked

Viewed 1,279 times

2

After executing this one, I came across this error, which did not display the API information. Whereas in another URL of a similar API the code displayed and worked perfectly.

API URL: https://economia.awesomeapi.com.br/json/all

$.ajax({
	type: "POST",
	dataType: "JSON",
	url: "https://economia.awesomeapi.com.br/json/all",
	success: function(data){
		console.log(data["USD"]["code"]);
	}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

inserir a descrição da imagem aqui

  • What’s the mistake? And transfer the code, [Edit] the question and don’t show an image, this makes it difficult to help your problem.

  • 1

    Test with GET instead of POST

  • It worked, thank you Caique Romero!

3 answers

4


You are receiving an error with HTTP code 405. This error code is described:

Method not allowed

That is, you are requesting a valid resource, but with a method that the server cannot respond to.

By opening the URL through the browser I was able to view the answer, this means that the feature accepts GET requests.

So just change the type of the requisition.

$.ajax({
  type: "GET",
  dataType: "JSON",
  url: "https://economia.awesomeapi.com.br/json/all",
  success: function(data){
    console.log(data["USD"]["code"]);
  }
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

1

Man, if I understand correctly you must be wanting to view the data returned in the correct API?

You can use the.log(data) console to view. And to access each attribute of the vc objects you can use object.attribute. In this case as the Response is a list vc you can use the for loop. For example:

       $.ajax({
            url:'https://economia.awesomeapi.com.br/json/all',
            method:'get',
            success:function(data){
                for(var i in data){
                    console.log(data[i].code);
                }
            }
        });

0

Man, I always use JSON.stringify() within a console.log() to show the result of an ajax call that returns a json. It would look like this:

$.ajax({
   type: "GET",
   dataType: "JSON",
   url: "https://economia.awesomeapi.com.br/json/all",
   success: function(data){
      console.log(JSON.stringify(data));
  }
});

Because this way you can visualize all the content and structure of JSON to be able to work with it.

Browser other questions tagged

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