ajax 仅在第一次通话期间调用 returns 'undefined'
ajax call returns 'undefined' during first call only
我正在尝试使用 datatable.in table 和键盘导航来制作类似文本输入帮助的桌面。
为此,我正在动态更改源并更改 header 列。到目前为止,除了获得动态列 header.
之外,我已经成功了
为了根据文本输入 header 获取列 header,我正在调用 ajax 并获取列 header 的列表。问题是在第一次调用 ajax returns undefined
期间,但在第二次调用中它显示了值。我知道这与异步调用有关,但不确定如何处理。
下面是我的代码片段。
AJAX 调用外部 .js
function ajaxCall(ipUrl, callType = "POST", dataIn) {
return $.ajax({
url: ipUrl,
type: callType,
data: dataIn,
dataType: "json",
success: function (response) {
retData = response.data;
alert('success'+ retData);
return retData;
}, error: function (err) {
alert("fail" + JSON.stringify(err));
}, //error
});
//alert('in js'+ retData);
//return retData;
}
HTML 脚本标签
$("#btn").click( function (e) {
var tData = { action: 'getHeader',
csrfmiddlewaretoken: 'Gys4TSx3ODJLcMDuXlSvS7DopVZr1HWEDLg9AlJsARXp7wmUGAxxKwo6uLVXIrf2',
}
tmp = ajaxCall('dyncolheader','GET',tData) ;
if (tmp == undefined) {
alert('second call');
tmp = ajaxCall('dyncolheader','GET',tData) ;
alert('tmp' + tmp);
} else {
alert('else' + tmp);
}
});
Django 查看代码
def dyncolheader(request):
hrdText = 'First,Second,Third'
if request.is_ajax and request.method == 'GET':
print('ajax call')
if request.GET.get('action') == 'getHeader':
print('get header')
return JsonResponse({ 'data': hrdText }, status=200)
return render(request, 'dyncolheader.html')
在 external.js 文件中使用此代码
function ajaxCall(ipUrl, callType = "POST", dataIn) {
console.log('function Call', ipUrl, callType, dataIn);
retData = null;
$.ajax({
url: ipUrl,
type: callType,
data: dataIn,
dataType: "json",
async: false,
success: function (response) {
retData = response.data;
},
error: function (err) {
alert("fail" + JSON.stringify(err));
},
});
console.log('retData', retData);
return retData;
}
我正在尝试使用 datatable.in table 和键盘导航来制作类似文本输入帮助的桌面。 为此,我正在动态更改源并更改 header 列。到目前为止,除了获得动态列 header.
之外,我已经成功了为了根据文本输入 header 获取列 header,我正在调用 ajax 并获取列 header 的列表。问题是在第一次调用 ajax returns undefined
期间,但在第二次调用中它显示了值。我知道这与异步调用有关,但不确定如何处理。
下面是我的代码片段。
AJAX 调用外部 .js
function ajaxCall(ipUrl, callType = "POST", dataIn) {
return $.ajax({
url: ipUrl,
type: callType,
data: dataIn,
dataType: "json",
success: function (response) {
retData = response.data;
alert('success'+ retData);
return retData;
}, error: function (err) {
alert("fail" + JSON.stringify(err));
}, //error
});
//alert('in js'+ retData);
//return retData;
}
HTML 脚本标签
$("#btn").click( function (e) {
var tData = { action: 'getHeader',
csrfmiddlewaretoken: 'Gys4TSx3ODJLcMDuXlSvS7DopVZr1HWEDLg9AlJsARXp7wmUGAxxKwo6uLVXIrf2',
}
tmp = ajaxCall('dyncolheader','GET',tData) ;
if (tmp == undefined) {
alert('second call');
tmp = ajaxCall('dyncolheader','GET',tData) ;
alert('tmp' + tmp);
} else {
alert('else' + tmp);
}
});
Django 查看代码
def dyncolheader(request):
hrdText = 'First,Second,Third'
if request.is_ajax and request.method == 'GET':
print('ajax call')
if request.GET.get('action') == 'getHeader':
print('get header')
return JsonResponse({ 'data': hrdText }, status=200)
return render(request, 'dyncolheader.html')
在 external.js 文件中使用此代码
function ajaxCall(ipUrl, callType = "POST", dataIn) {
console.log('function Call', ipUrl, callType, dataIn);
retData = null;
$.ajax({
url: ipUrl,
type: callType,
data: dataIn,
dataType: "json",
async: false,
success: function (response) {
retData = response.data;
},
error: function (err) {
alert("fail" + JSON.stringify(err));
},
});
console.log('retData', retData);
return retData;
}