gpt4 book ai didi

javascript - ajax 响应中无法访问变量

转载 作者:行者123 更新时间:2023-11-28 10:57:58 25 4
gpt4 key购买 nike

我在 ajax 响应中使用全局变量时遇到问题。LastDate 是我加载到第二页的页面中定义的变量。 (函数load_table)

我能够在ajax调用之前访问该变量,但我似乎无法在ajax成功中访问它。因为它给出了未定义的。 <====在代码中

我的代码:

var dia_date = {};

$(window).load(function()
{
DP("eerste keer")
load_table();
} );

function load_table()
{
DP('load_table');
$.ajax({
type: "POST",
url: "/diagnose_hoofdpagina/table_diagnose/" + DosierID,
success: function (data) {
$("#diagnoses_zelf").html('');
$("#diagnoses_zelf").append(data).trigger('create');
//initialize_table();
update_table();
},
error: function(){
alert('error');
}
});
return false;
}



function update_table()
{
if(LastDate > Datum)
{
alert("LasteDate" + LasteDate);
}
else
{
alert("Datum" + Datum);
}

alert('gast .... ' + LastDate); // <========== this is promted on the screen so there is no problem
$.ajax({
type: "POST",
url: "/refresh_diagnose/" + DosierID,
dataType: "json",
data : JSON.stringify(dia_date),
success: function (data) {
var DataDate = new Date(data.Year, data.Month, data.Day, data.Hour, data.Minute, data.Second);
alert('lastdate :'+ LastDate + 'date.date :' + DataDate);

//<============ BUT HERE HE GIVES LastDate AS UNDEFINED

},
error: function(data){
alert(data);
}
});
return false;
}

我看不出我做错了什么。有人可以帮我吗?提前致谢。

最佳答案

你可以尝试创建一个函数。

var lastDate = #;
function getLastDate(){return lastDate;}

ajax.blablabla.success :{getLastDate();}

关于javascript - ajax 响应中无法访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16776574/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com