gpt4 book ai didi

javascript - 第一次函数返回未定义,其他时候它有效吗?使用 Jquery、Javascript、Ajax

转载 作者:行者123 更新时间:2023-11-28 06:46:02 25 4
gpt4 key购买 nike

第一次函数返回未定义,每次它工作并返回对象....

基本上,第一次存储 var _d 时,它是一个未定义的值。

第一次存储其值时,单击是“未定义”的,并且每隔一次它都会存储适当的值。

// jscript.js

function getDataById(This,table,Url)
{
var Id = table.row(This.parent().parent()).data().id;
$.ajax({
url: Url + "/" + Id,
type: "GET",
dataType: 'json',
success: function (Data) {
var _d = Data;
return _d;
},
error: function () {
sweetAlert("Oops...", "Something went wrong!", "error");
}
});

}

/***********************/

$(document).ready(function () {

$("#test tbody").on('click', '#aff', function () {
console.log( getDataById($(this),table,"test/email") );
});

)};

/*****************/

// undefined

最佳答案

$.ajax() 是异步的,success 只是一个回调。函数 getDataById 必须有一个回调,该回调将在 ajax 请求后调用

// jscript.js

function getDataById(This, table, Url, callback) {
var Id = table.row(This.parent().parent()).data().id;
$.ajax({
url: Url + "/" + Id,
type: "GET",
dataType: 'json',
success: function (Data) {
callback(Data);
},
error: function () {
sweetAlert("Oops...", "Something went wrong!", "error");
}
});
}

/***********************/

$(document).ready(function () {
$("#test tbody").on('click', '#aff', function () {
getDataById($(this), table, "test/email", function (data) {
console.log(data);
});
});
)
};
/*****************/

关于javascript - 第一次函数返回未定义,其他时候它有效吗?使用 Jquery、Javascript、Ajax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33414211/

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