Ajax/jQuery 将变量数据传递给另一个 jQuery?

发布于 2025-01-14 19:39:51 字数 1143 浏览 0 评论 0原文

我有两个 jQuery 调用,一个通过 IP 获取访问者城市,另一个在网站上打开一个弹出窗口。我有一个问题,首先调用获取客户端城市,但我不能,不知道如何将其传递给第二个查询。有谁知道该怎么做?

client_city 需要传递给这个查询,因为 city=client_city

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

我尝试了各种方法,但为什么第一次调用不会将变量数据传递到其函数之外,有什么想法吗?

// 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();
    });
});

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();
    });
});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖树树初阳… 2025-01-21 19:39:51

只需为要在其他函数中使用的数据创建一个全局变量即可。

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

初与友歌 2025-01-21 19:39:51

像这样尝试过,但仍然没有成功。

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)

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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文