How do I display the API JSON?

2

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

API URL: link

$.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>

    
asked by anonymous 30.10.2017 / 17:00

3 answers

4

You are getting an error with the HTTP 405 code. This error code has as a description:

  

Method not allowed

That is, you are making the request for a valid resource, but with a method that the server can not respond to.

Opening the URL through the browser I was able to visualize the response, which means that the feature accepts GET requests.

Then just change the type of the request.

$.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>
    
30.10.2017 / 17:31
0

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

You can use console.log (data) to view. And to access each attribute of the objects you can use attribute.attribute. In this case, as the response is a list 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);
                }
            }
        });
    
30.10.2017 / 17:33
0

Dude, I always use JSON.stringify() inside 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));
  }
});

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

    
30.10.2017 / 20:18