Load fields automatically with jquery and json

2

I need to fill in my fields after selecting one.

My JSON is as follows:

[{"codeVisit":"3EE","segmentVisit":"industria","streetVisit":"Rua Francisco Leandro Cunha","neighbVisit":"Vila","countryVisit":"Brasil","client":"5580262b600e53e82069bbeb"}]

And it's at: /clienteJson

I have the fields with the names below in my HTML ( input text , except the client which is a select ):

The client field is a select and after selecting it, I need the other data to be loaded automatically.

I'm doing a script as follows:

$(document).ready(function(){
    $("select[name='client']").change(function(){
      var codeVisit= $("input[name='codeVisit']");
      var segmentVisit = $("input[name='segmentVisit']");
      $(codeVisit).val('Carregando...');
      $(segmentVisit).val('Carregando...');
        $.getJSON(
        "/clienteJson"          
        { client: $( this ).val() },
          function( json )
          {
            $( codeVisit ).val( json.codeVisit );
            var s = $( segmentVisit ).val( json.segmentVisit );
            console.log(s)
          }
        );
        alert("erro")
    });
  });

But the fields are not loaded.

What can I do?

I'm using nodejs and mongoose.

    
asked by anonymous 16.06.2015 / 19:54

2 answers

2
$.getJSON(
   "/clienteJson",         
   { client: $( this ).val() },
   function( json ){
      codeVisit.val( json[0].codeVisit );
      segmentVisit.val( json[0].segmentVisit );
   }
);

Edit 1 -----------------

The response object is an array of a single element, hence:

array[0].propriedade

For more information on json syntax, visit: link

    
07.08.2015 / 20:28
1

Resolved as follows:

$(document).ready(function() {
    $("select[name='client']").change(function() {
        $.getJSON(
            "/clientsjson/" + this.value + "",
            function(json) {
                $("#codeClient").val(json.code);
                $("#segmentClient").val(json.segment);
                $("#sectorClient").val(json.sector);
                $("#industryClient").val(json.industry);
            }
        );
    });
});
    
11.08.2015 / 15:39