检查Ajax响应数据为空/空白/空/未定义/ 0 [英] Check if AJAX response data is empty/blank/null/undefined/0

查看:590
本文介绍了检查Ajax响应数据为空/空白/空/未定义/ 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的一个错误!);
    }
});
 

我的问题:

我已经试过了各种条件,但没有正确地检查数据。根据我的研究结果,一个空白的警告信息,并不意味着数据

  1. 不存在
  2. 等于零
  3. 零长度
  4. 未定义

如果这是没有这些东西,我怎么能因此有条件地检查能够产生一个空白的警报消息中的数据?

解决方案

  //如果(数据=未定义){
 

这是一个赋值语句,而不是一个比较。此外,未定义是一个字符串,它是一个属性。检查是这样的:如果(数据==未定义)(没有引号,否则它是一个字符串值)

如果它没有定义,你可能会返回一个空字符串。你可以尝试检查一个 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

  1. empty
  2. non-existent
  3. equal to zero
  4. of length zero
  5. null
  6. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆