检查Ajax响应数据为空/空白/空/未定义/ 0 [英] Check if AJAX response data is empty/blank/null/undefined/0
问题描述
什么我有:
我有jQuery的AJAX功能查询数据库后返回的HTML。根据查询结果,所述函数将或者返回HTML code或没有(即空白),为期望的。
我需要什么:
我需要有条件地检查时数据是空白。
我的code:
$。阿贾克斯({
键入:POST,
网址:?< PHP的回声ADMIN_URL(管理-ajax.php');>中,
数据:associated_buildsorprojects_form,
成功:功能(数据){
如果(!数据){
//如果(数据=未定义){
//如果(数据===未定义){
//如果(数据== NULL){
//if(data.length == 0){
//如果(data.length!= 0){
//如果(数据=== 0){
//如果(数据===0){
警报(数据:+数据);
}
},
错误:函数(errorThrown){
警报(errorThrown);
警报(有是AJAX的一个错误!);
}
});
我的问题:
我已经试过了各种条件,但没有正确地检查数据。根据我的研究结果,一个空白的警告信息,并不意味着数据
- 空
- 不存在
- 等于零
- 零长度 的
- 空
- 未定义
如果这是没有这些东西,我怎么能因此有条件地检查能够产生一个空白的警报消息中的数据?
//如果(数据=未定义){
这是一个赋值语句,而不是一个比较。此外,未定义
是一个字符串,它是一个属性。检查是这样的:如果(数据==未定义)
(没有引号,否则它是一个字符串值)
如果它没有定义,你可能会返回一个空字符串。你可以尝试检查一个 falsy
值如如果(!数据)
以及
What I have:
I have jQuery AJAX function that returns HTML after querying a database. Depending on the result of the query, the function will either return HTML code or nothing (i.e. blank) as desired.
What I need:
I need to conditionally check for when the data is blank.
My code:
$.ajax({
type:"POST",
url: "<?php echo admin_url('admin-ajax.php'); ?>",
data: associated_buildsorprojects_form,
success:function(data){
if(!data){
//if(data="undefined"){
//if(data==="undefined"){
//if(data==null){
//if(data.length == 0){
//if ( data.length != 0 ){
//if(data===0){
//if(data==="0"){
alert("Data: " + data);
}
},
error: function(errorThrown){
alert(errorThrown);
alert("There is an error with AJAX!");
}
});
My problem:
I've tried a variety of conditions but none correctly check the data. Based on my findings, a blank alert message does not mean data is
- empty
- non-existent
- equal to zero
- of length zero
- null
- undefined
If it's none of these things, how can I therefore conditionally check for the data that yields a blank alert message?
//if(data="undefined"){
This is an assignment statement, not a comparison. Also, "undefined"
is a string, it's a property. Checking it is like this: if (data == undefined)
(no quotes, otherwise it's a string value)
If it's not defined, you may be returning an empty string. You could try checking for a falsy
value like if (!data)
as well
这篇关于检查Ajax响应数据为空/空白/空/未定义/ 0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!