jQuery 检查AJAX响应是否为空等问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3503014/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Checking to see if AJAX response is empty and other problems
提问by LiamGu
I'm currently trying to check if the response I'm getting is empty. Now what I think will work is below:
我目前正在尝试检查我得到的响应是否为空。现在我认为会起作用的是:
$.ajax({
type: 'GET',
url: '<%=Url.Action("FindTransaction", "Calls") %>',
data:
{ companyID: $('#CompanyDDL').val(),
storeID: storeNo,
tranDate: $('#TranDate').val(),
tranNum: $('#TranNum').val()
},
success: function (tData) {
if (tData == null) {
$('#tranNotFound').show("blind", options, 500);
} else {
$('#products').html('');
$('#SKUs').html('');
$('#price').html('');
for (var i = 0; i < tData.length; i++) {
$('#SKUs').append(!tData ? '' : tData[i].SKUN + '<br />');
$('#products').append(!tData ? '' : tData[i].DESCR + '<br />');
$('#price').append(!tData ? '' : tData[i].EXTP + '<br />');
}
$('#till').html(!tData ? '' : tData[0].TILL);
$('#tran').html(!tData ? '' : tData[0].TRAN);
$('#cashier').html(!tData ? '' : tData[0].CashierName);
$('#total').html(!tData ? '' : tData[0].TOTL);
$('#fullTransactionDetails').show("blind", options, 500);
}
}
});
I think what I'm doing will achieve what I'm aiming for however, I can't seem to find out as I'm having a second issue of tData[0] is undefined
and I'm trying to fetch data for something that I know will definately return an empty response, so as far as I'm concerned, it shouldn't even hit that part of the code.
我认为我正在做的事情将实现我的目标,但是,我似乎无法找到,因为我有第二个问题,tData[0] is undefined
并且我正在尝试为我知道肯定会返回的内容获取数据一个空的响应,就我而言,它甚至不应该触及代码的那部分。
I'm at a bit of a loss with this so any help is greatly appreciated.
我对此感到有些茫然,因此非常感谢任何帮助。
回答by David Hoerster
If you're falling into the success handler of your $.ajax
call, you're probably getting an empty object literal back (if it's a JSON dataType being returned). So you're null check is failing because it really isn't null -- it's empty.
如果您陷入$.ajax
调用的成功处理程序中,您可能会得到一个空的对象文字(如果它是一个 JSON 数据类型被返回)。所以你的空检查失败了,因为它真的不是空的——它是空的。
Here's a sample of what may be going on:
以下是可能发生的情况的示例:
$(document).ready(function() {
var x = {};
if (x==null) {
alert("I am null");
} else {
alert(x);
}
if ($.isEmptyObject(x)) {
alert("I am empty");
} else {
alert(x);
}
});
In the first test, the null check will fail and you'll get an alert of 'object [Object]'. But the second test will succeed and you'll get the 'I am empty' alert.
在第一个测试中,空检查将失败,您将收到“对象 [对象]”的警报。但是第二次测试会成功,您会收到“我是空的”警报。
Here's a link to it on jsFiddle: http://jsfiddle.net/pcdP2/2/
这是 jsFiddle 上的链接:http: //jsfiddle.net/pcdP2/2/
$.isEmptyObject() is in jQuery 1.4 (per the jQuery API), so it won't be available if you're not on that version.
$.isEmptyObject() 在 jQuery 1.4 中(根据 jQuery API),因此如果您不在该版本上,它将不可用。
回答by user984003
What worked for me was:
对我有用的是:
if ( data.length != 0 )
回答by Mukesh
Trimming the data for white spaces worked for me.
修剪空白的数据对我有用。
jQuery.get(url,{parameters},function(data){
data=data.trim();
if (data) {
alert('Data available')
} else {
alert('Empty')
}
});
回答by Ed Schembor
I believe the success function will be passed a non-null string, even when no data is returned - so, you may need to check for tData == '' (in addition to checking tData==null)
我相信成功函数将传递一个非空字符串,即使没有返回数据 - 因此,您可能需要检查 tData == '' (除了检查 tData==null )
回答by jeffday
Oddly enough when encountering this situation I found that this comparison works:
奇怪的是,当遇到这种情况时,我发现这种比较有效:
$.ajax({url:'a url',
dataType:'html',
success: foo
});
function foo(data) {
if (data && data != " ") {
console.log("response was empty");
}
}
Neither !data or $.isEmptyObject(data) identified a blank response correctly. I'm using jQuery 1.7.1.
!data 或 $.isEmptyObject(data) 都没有正确识别空白响应。我正在使用 jQuery 1.7.1。
回答by Africa Matjie
i suggest you convert the return result into a string then start validating, bcos ajax might be returning a certaing datatype that u cant validate: Try this
我建议您将返回结果转换为字符串然后开始验证,bcos ajax 可能会返回您无法验证的特定数据类型:试试这个
succes:function(resultvalue)
{
var result = resultvalue.toString();
if(result=='')
{
alert('the result is empty');
}
else
{
alert('the result is not empty');
}
}