skip to Main Content

I have two jQuery calls, one to get visitor city by IP, another which opens a pop-up on a website. I have a problem, first call get the client city but I can not, not sure how to pass it to the second query. Does anyone know how to do it?

client_city needs to be passed to this query, as city=client_city

hidden: 'utm_source,utm_medium,utm_campaign,utm_term,utm_content'

I tried various ways, but somewhy first call doesn’t pass the variable data outside its function, any ideas?

// First Call to get City
jQuery.ajax({
  type: "GET",
  dataType: "json",
  url: 'https://ip-api.io/json?api_key=****',
  success: function(data) {
  client_city=data.city
  console.log(client_city)
  }
});
// Second Call to create pop
jQuery(function($){
    
    $('#menu-item-37 a').click(function (e) {
        e.preventDefault();
        var popup = window.tf.createPopup('sxmSCYSc', {
            hideHeaders: true,
            transitiveSearchParams: ['utm_source', 'utm_medium', 'utm_campaign', 'utm_term', 'utm_content'],
            iframeProps: {
                title: 'Dermatologo konsultacija internetu - iDerma'
            },
            medium: 'snippet',
            hidden: 'utm_source,utm_medium,utm_campaign,utm_term,utm_content'
        });
        popup.toggle();
    });
});

2

Answers


  1. Chosen as BEST ANSWER

    Tried it like this, but still no success.

    var client_city = ''
    
    jQuery.ajax({
      type: "GET",
      dataType: "json",
      url: 'https://ip-api.io/json?api_key=****',
      success: function(data) {
      client_city = data.city
    
      
      }
    });
    
    console.log(client_city)
    

  2. just make a global variable for the data that you want to use in the other function.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search