gpt4 book ai didi

javascript - 在函数之间传递变量

转载 作者:行者123 更新时间:2023-11-30 13:20:24 25 4
gpt4 key购买 nike

如果我有两个从 ajax 调用的函数我想将第一个函数返回的内容传递给另一个函数中的一个变量,并且只使用一次 ONLY ONE TIME

带有 returnResult() 函数的下面的代码循环 $.each()并且缺少一些 json 数据,就像“labe”中没有文本一样:{}

 $.ajax({
type: "POST",
url: "WebService.asmx/MyEventData",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
returnResult(data.d);
}

});

$.ajax({
type: "POST",
url: "WebService.asmx/Getlines",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data) {
lineReturn(data.d);
}

});



});

function lineReturn(result) {
var jsonObj = [] ;
$.each(result, function(index, value) {
jsonObj.push({ "value": value.dateTime, "color": '#D2691E', "width": 1, "label": { "text": value.LineEvent} });
});
return JSON.stringify(jsonObj) ; << this is the return that should go to myline variable and be used on the highchart >>>>

}

function returnResult(result) {
console.log(result) ;
var myLine = lineReturn(result) ;
var datetemp = new Array ;
var dateflow = new Array ;
var datepressure = new Array;
var datecond = new Array ;

最佳答案

尝试将第二个 ajax 调用放入第一个的回调中。

$.ajax({
type: "POST",
url: "WebService.asmx/Getlines",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(dataA) {
$.ajax({
type: "POST",
url: "WebService.asmx/MyEventData",
data: "{}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(dataB) {
// From here you can access both dataA.d and dataB.d
var myLineA = lineReturn(dataA.d);
var myLineB = lineReturn(dataB.d);
}
});
}
});

关于javascript - 在函数之间传递变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10402021/

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