Load fields automatically with jquery and json

Asked

Viewed 1,001 times

2

I need to fill in my fields after selecting a.

My JSON is as follows:

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

And it’s at the address: /clienteJson

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

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

I’m making 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 fields are not loaded.

What can I do?

I’m using nodejs and Mongoose.

  • You looked at the Javascript console to see the error message? /clienteJson should be a string, and should be succeeded by a comma.

  • Besides what @ctgPi said, I also didn’t understand why you take inputs and arrow loading

  • Now I noticed also that your json starts [{"codeVisit":3EE, …; are missing quotes around 3EE. How exactly are you generating this JSON there on the server?

  • The json is correct, I edited and forgot to include the quotation marks of 3EE, but the json has. and about the /clientJson, I put in ''" and comma. Still it didn’t work. The big problem is that my Json file mounts all the information (if it has 200, it mounts 200 arrays) . When sending {client: $(this). val}, it searches for the specific ID, so much so that the error in the console is: http://localhost:3000/clientJson? client=5580262b600e53e82069bbeb Not Found.... I wanted to know how to find the information and my json brings everything.

  • I see you’re correcting code errors... is the rest of the code what you’re using? or is it sample code? For example client: $( this ).val() }, is wrong... the problem is described here: http://answall.com/a/3325/129

  • I just fixed it. Now it’s okay! Yes the problem is with the client: $( this ).val() because as I said my Json file, the url opens with all the data... I did not create a URL with ID. and this client:$(this), searches the id . I wanted to know how to search the data, without having to pass this ID. That is, when I select(select client), search in my json file the data and fill the others.

Show 1 more comment

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, so:

array[0].propriedade

For more information about json syntax, visit: http://json.org

  • What does this code do? Could you complete your answer by explaining how it is able to solve the question?

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);
            }
        );
    });
});

Browser other questions tagged

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