Skip to content Skip to sidebar Skip to footer

Variable Lost In Ajax Request

I'm facing a strange behaviour when trying to pass a variable as parameter to a nested ajax request callback: $('form').on('submit',function(e){ $.ajaxSetup({ header:$(

Solution 1:

The first argument passed to the function in done is the response from the AJAX request. It doesn't matter what you call the variable, that's what will be passed to that function.

You can capture the value in the closure however, simply give it another name and assign it to a local variable. Something like this:

done(function(response) {
    var theId = id;

    // "response" contains the response from the server.// "theId" contains the value of `id` from outside this function.
})

Solution 2:

The parameter of the .done() method is the response of the AJAX call. If your call returned a HTML page, the id variable got all of the html string assigned to it.

To keep the id in its variable simply use another one like:

.done(function(data) {
  console.log(data)
  console.log(id); 
});

Post a Comment for "Variable Lost In Ajax Request"