1

I have the javascript code below that POSTs a request, I then want to save the response to a local variable named sku, but it is not binding.

the variable sku remains an empty string.

var sku = "";  //to be passed to server when filled
    
    var cookie_info = {
        number_nights: number_nights_cookie,
        number_travelers: number_travelers_cookie,
        type_traveler: type_traveler_cookie,
        departure_date: departure_date_cookie,
        country: country_cookie
    };
    
    $.ajax({
         type: 'POST',
         url: 'https://azooree.com/wp-admin/php/sku-calculation.php',
         data: cookie_info,
         success: function(response) {               
            console.log('success', response);
            sku = response;
         },
         error: function() {
             console.log('error!');
             alert("[ERROR] Information lost, please try again. Thank you!");
         }
    });
    
    var basic = {
        info: sku + "LP" 
    };

1 Answers1

2

The value of variable sku is being returned from your POST request which is asynchronous. That's why at the time of assigning the value of sku to basic - it will be empty.

You can easily update the value of basic either by using a callback function like below-

$.ajax({
     type: 'POST',
     url: 'https://azooree.com/wp-admin/php/sku-calculation.php',
     data: cookie_info,
     success: function(response) {               
        console.log('success', response);
        sku = response;
        updateBasicValue(sku);
     },
     error: function() {
         console.log('error!');
         alert("[ERROR] Information lost, please try again. Thank you!");
     }
});
function updateBasicValue(sku) {
    basic = {
        info: sku+"LP"
    };
}

Or else, you can do whatever you need to do with the sku variable inside the success function of your AJAX request.

UPDATE You also need to check for CORS issues if your request is being sent from any other domain or subdomain different than the one causing it. - (Thanks LordNeo)

Shekhar Chikara
  • 3,786
  • 2
  • 29
  • 52
  • Just add that he needs to check for CORS issues if the request is being sent from any other domain or subdomain different than the one causing it. – LordNeo Oct 02 '17 at 18:53
  • That does not seem to work. How do I make the script wait for the response before continuing? – Igor Silveira Oct 02 '17 at 18:53
  • @IgorSilveira The script cannot wait for the response - this is the way AJAX calls work. What do you need to do with your response? – Shekhar Chikara Oct 02 '17 at 18:56
  • I need to use to make a GET request – Igor Silveira Oct 02 '17 at 19:01
  • So you can write your `GET` request in the `updateBasicValue()` function in my example above. – Shekhar Chikara Oct 02 '17 at 19:03
  • I think I can do what I want if I respond to the request with an array. And perhaps it has better performance than a request inside the request ? – Igor Silveira Oct 02 '17 at 19:23
  • I am not really sure what you mean by 'respond to the request with an array'. Also, performance-wise I don't think you can do much if you need to make a GET request based on the data you get from your POST request. – Shekhar Chikara Oct 02 '17 at 19:25