0

I have a script that basically check if there is a result from a query:

$.post('<?php echo site_url('xxx/yyy'); ?>',{data:no},function(result){
        if(!result){
            alert("No Data");
        }
        else{
            alert("Data retrieved");
        }
});

Why my IF alert Data retrieved when I get empty JSON array. I tried to do alert(result) with different data (the data which result is empty and result is true), but both data is alerting Data retrieved.

This is my model:

$this->db->select('*',FALSE);
    $this->db->from('t_penomoran tp');      
    $this->db->join('t_penomoran_detail t_pd', 'tp.nomor = t_pd.nomor');

    $this->db->where('tp.nomor',$nomor);

    $query = $this->db->get();
    return $query->result_array();

note:
For some reason when I do alert(result.length) with data that has no value, the result is 2. But when I do alert(result) with data that has no value the result is []

4
  • Empty JSON array [] in stringified form is still string, so maybe you need to parse the result to actual JSON first? Commented Oct 24, 2017 at 4:51
  • @DanielH.J. "JSON (JavaScript Object Notation) is a lightweight data-interchange format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others." - What is the difference between JSON and Object Literal Notation? Commented Oct 24, 2017 at 4:57
  • 1
    Andreas, what I am saying is the Ajax result is an empty JSON array that is stringified, so [] is not the same as '[]'. '[]' is a string with length of 2. You need to parse the result first. Commented Oct 24, 2017 at 5:07
  • @DanielH.J. If the server doesn't send application/json as the content-type you're right. Otherwise jQuery will automatically parse the response (jsfiddle.net/Lpt9n8q3). Commented Oct 24, 2017 at 10:48

3 Answers 3

1

Empty array is a truthy value, meaning in your if statement, it will evaluate to true and that is why you see the alert(). To fix it, use the length property:

if(!result.length) alert('no data')
Sign up to request clarification or add additional context in comments.

2 Comments

still no luck. For some reason when I do alert(result.length) with data that has no value, the result is 2. But when I do alert(result) with data that has no value the result is []
That correlates to result being a string of length 2, namely”[]”
0

You can check like this

 if(jQuery.isEmptyObject(result))
{
        alert('no data')
}

Comments

0

First of all parse your result like this

var res = result.responseJSON;

Then alert what you get if empty then it will be null. so you can filter that out

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.