Variable within GET Ajax Request

Asked

Viewed 828 times

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 take this variable and treat the return, correct ? happens that when placing the variable concatenating in the url I can’t get any result the return I get is "Undefined", if I put a static value for example a number: url: "http://localhost/projectohtml/admin/users-update/55/update" then I can have the return as 55, what do I need to do for the variable to be 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 () {

            }
        });

1 answer

1


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

Tries to replace the $.getJSON all over:

$.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 case of success of AJAX 1, so you do not need the global variable ID, 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);
        }
    });

});
  • hmm understood, strange that I put a console.log in the answer and it comes with the value of the id inside even ajax, but you would be able to send me an example of how it would look with this false parameter ?

  • I updated the answer, I’m there

  • I got here guy worked super well, that’s just what I needed, I was tying everything via Ssion, now picking up the ID will be much easier, there’s only one thing I just got this log : jquery.min.js:4 [Deprecation] Synchronous Xmlhttprequest on the main thread is deprecated because of its detrimental effects to the end user’s Experience. For more help, check https://xhr.spec.whatwg.org/.

  • Right! Now you’ll need to put a $.ajax into the other’s Success property! So you take out the async: false, I’ll update the answer

  • Beauty I’m waiting Thanks !

  • Vish the only error that gave the first console.log of the first ajax and the second brings another id, I’m trying to figure out why.

  • Try to date quote: JSON by date: 'json'

  • I discovered here the get ta always picking up the same id it seems I have to see why, strange that the first time it ran worked.

  • Forget I discovered here kkkkkkkk Orelhada my auhahuahuahu

Show 4 more comments

Browser other questions tagged

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