我有jQuery的AJAX功能查询数据库后返回的HTML。根据查询结果,所述函数将或者返回HTML code或没有(即空白),为期望的。
我需要有条件地检查时数据是空白。
$。阿贾克斯({
键入:POST,
网址:?< PHP的回声ADMIN_URL(管理-ajax.php');>中,
数据:associated_buildsorprojects_form,
成功:功能(数据){
如果(!数据){
//如果(数据=未定义){
//如果(数据===未定义){
//如果(数据== NULL){
//if(data.length == 0){
//如果(data.length!= 0){
//如果(数据=== 0){
//如果(数据===0){
警报(数据:+数据);
}
},
错误:函数(errorThrown){
警报(errorThrown);
警报(有是AJAX的一个错误!);
}
});
我已经试过了各种条件,但没有正确地检查数据。根据我的研究结果,一个空白的警告信息,并不意味着数据
空 不存在 等于零 零长度的 空 未定义如果这是没有这些东西,我怎么能因此有条件地检查能够产生一个空白的警报消息中的数据?
解决方案 //如果(数据=未定义){
这是一个赋值语句,而不是一个比较。此外,未定义
是一个字符串,它是一个属性。检查是这样的:如果(数据==未定义)
(没有引号,否则它是一个字符串值)
如果它没有定义,你可能会返回一个空字符串。你可以尝试检查一个 falsy
值如如果(!数据)
以及
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.
I need to conditionally check for when the data is blank.
$.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!");
}
});
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 undefinedIf 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