API error FIPE table

Asked

Viewed 1,781 times

0

I am using an API to consume data from the FIPE table for filling a form. I can consume the FIPE field Brand and Year, however, the vehicle field of error.

Documentation link: https://deividfortuna.github.io/fipe/

Follows the code:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Documento sem título</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
	$(document).ready(function() {
  var urlBase = "https://fipe.parallelum.com.br/api/v1/carros/marcas";
  
/** Marcas**/ 
		
		$.getJSON(urlBase, function(data) {
    var items = ["<option value=\"\">ESCOLHA UMA MARCA</option>"];
    $.each(data, function(key, val) {
      items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
    });
    $("#marcas").html(items);
  });
  
		/** Veiculo**/ 
	
		$("#marcas").change(function() {
    $.getJSON(urlBase + "/" + jQuery("#marcas").val() + "/" + "modelos", function(data) {
      var items = ["<option value=\"\">ESCOLHA UM VEICULO</option>"];
      $.each(data, function(key, val) {
        items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
      });
      $("#modelos").html(items);
    });
  });
  
		/** Ano**/ 
		
		$("#veiculos").change(function() {
    $.getJSON(urlBase + "/" + jQuery("#marcas").val() + "/" + "modelos" + "/" + jQuery("#modelos").val() + "/" + "anos", function(data) {
      var items = ["<option value=\"\">ESCOLHA O ANO</option>"];
      $.each(data, function(key, val) {
        items += ("<option value='" + val.id + "'>" + val.name + "</option>");
      });
      $("#ano").html(items);
    });
  });
});
	</script>
</head>

<body>
  <select id="marcas">
        </select>
  <select id="modelos">
        </select>
  <select id="ano">
        </select>
</body>
</html>

If anyone can shed a light, I’ll be very grateful. Thank you.

  • puts the code in the body of the question

  • Done! Thanks for your attention.

1 answer

4


Try this code:

    $(document).ready(function() {
      var urlBase = "//fipe.parallelum.com.br/api/v1/carros/marcas";

      /** Marcas**/

      $.getJSON(urlBase, function(data) {
        var items = ["<option value=\"\">ESCOLHA UMA MARCA</option>"];
        $.each(data, function(key, val) {
          items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
        });
        $("#marcas").html(items);
      });

      /** Veiculo**/

      $("#marcas").change(function() {
        $.getJSON(urlBase + "/" + jQuery("#marcas").val() + "/" + "modelos", function(data) {
          var items = ["<option value=\"\">ESCOLHA UM VEICULO</option>"];
          $.each(data.modelos, function(key, val) {
            items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
          });
          $("#modelos").html(items);
        });
      });

      /** Ano**/

      $("#modelos").change(function() {
        $.getJSON(urlBase + "/" + jQuery("#marcas").val() + "/" + "modelos" + "/" + jQuery("#modelos").val() + "/" + "anos", function(data) {
          var items = ["<option value=\"\">ESCOLHA O ANO</option>"];
          $.each(data, function(key, val) {
            console.log(data)
            items += ("<option value='" + val.codigo + "'>" + val.nome + "</option>");
          });
          $("#ano").html(items);
        });
      });
    });
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <title>Documento sem título</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>

<body>
  <select id="marcas">
  </select>
  <select id="modelos">
  </select>
  <select id="ano">
  </select>
</body>

</html>

Neat stitches:

It was arranged that it was not calling the array;

{"modelos":[{"nome":

Begins with models.

So when calling, you need to tell which array;

data.modelos

Another thing, in $('#veiculos') I switched to $("#modelos").change(function() {

  • 1

    It worked!!! Thank you very much, saved me :))

  • I also paid attention where I was wrong, was learning!

  • @Phillipferreira when he’s in trouble, play a console.log(data), and access the API link also to see the arrays it calls.

Browser other questions tagged

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