Ajax/jQuery 将变量数据传递给另一个 jQuery?
我有两个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需为要在其他函数中使用的数据创建一个全局变量即可。
just make a global variable for the data that you want to use in the other function.
像这样尝试过,但仍然没有成功。
Tried it like this, but still no success.