如果我有两个从ajax调用的函数 我想传递第一个函数返回到另一个变量的东西,并且只使用它一次只有一次
使用returnResult()函数$ .each()的代码bellow循环 并且一些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 ;
答案 0 :(得分:2)
尝试将第二个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);
}
});
}
});
答案 1 :(得分:1)
您可以在第一个函数的.success
上使用第二个函数。