Variable within GET Ajax Request

0

Good afternoon, I need a help, I have this request get from ajax that sends to the web service, and I need to put this variable id, in the url, because inside the webservice I get this variable and I treat the return, correct ? I do not know how to do this, but I'm not sure how to do this, but I'm not sure how to do this. / users-update / 55 / update "> link " so I can get the return as 55, what do I need to do to get the variable recognized? Thank you.

 $(document).ready(function(){

        var id;


       $.getJSON('http://localhost/projetohtml/admin/users-list-all',function(data){
                 $.each(data, function(k, v){

                   id = v.iduser;
                   console.log(id);

                    });
              });

     $.ajax({
            url: "http://localhost/projetohtml/admin/users-update/"+id+"/update",
            method: 'GET',
            data: JSON,
            success: function ( response ) {
              console.log(response);
              console.log(id);

              $.each(JSON.parse(response), function(k, v){


                  $("#desperson").attr("value",v.desperson);
                   $("#deslogin").attr("value",v.deslogin);
                    $("#nrphone").attr("value",v.nrphone);
                    $("#desemail").attr("value",v.desemail);
                     $("#inadmin").attr("value",v.inadmin);


              });


               /*window.location.replace("http://localhost/projetohtml/admin/users/users-update")*/
            },
            error: function () {

            }
        });
    
asked by anonymous 21.06.2017 / 19:56

1 answer

1

Man, the $. getJSON method is asynchronous. That is, you end up using the id in the $ ajax method before the previous method sets a value for the ID variable. Try to make a $ ajax with async: false instead of $ getJSON , thus the second method $. ajax is not started before the end of the first one.

Try to replace $. getJSON with:

$.ajax({
    url: 'http://localhost/projetohtml/admin/users-list-all',
    async: false,
    type: 'get',
    dataType: 'json',
    success: function(data){
        $.each(data, function(k, v){
            id = v.iduser;
            console.log(id);
        });
    },
    error: function(error){
        console.log(error)
    }
});

Or with everything asynchronous, note that AJAX 2 only runs in the case of AJAX 1 success, so it does not need the global ID variable, it is local:

$(document).ready(function(){

    $.ajax({
        url: 'http://localhost/projetohtml/admin/users-list-all',
        type: 'get',
        dataType: 'json',
        success: function(data){
            var id;
            $.each(data, function(k, v){
                id = v.iduser;
                console.log(id);
            });

            //Segundo AJAX
            $.ajax({
                url: "http://localhost/projetohtml/admin/users-update/"+id+"/update",
                method: 'GET',
                data: JSON,
                success: function ( response ) {
                    console.log(response);
                    console.log(id);

                    $.each(JSON.parse(response), function(k, v){
                        $("#desperson").attr("value",v.desperson);
                        $("#deslogin").attr("value",v.deslogin);
                        $("#nrphone").attr("value",v.nrphone);
                        $("#desemail").attr("value",v.desemail);
                        $("#inadmin").attr("value",v.inadmin);
                    });
                },
                error: function (error) {
                    console.log('Erro AJAX 2: ' + error);
                }
                //Fim do segundo AJAX
            });
        },
        error: function(error){
            console.log('Erro AJAX 1: ' + error);
        }
    });

});
    
21.06.2017 / 21:23