-1

I try this:

$.post('blog.php',
    {   },
    function(data,status){
        if(data && data != "")
        {
            $("#readmore").before(data);
        }
        else
        {
            alert('no more text');
        }
});

Normally it works. but when blog.php is empty do not run else condition.

the content of blog.php is text.

partiz
  • 1,206
  • 2
  • 13
  • 34
  • 1
    And what's wrong with your code? What data looks like when it is 'empty'??? – A. Wolff Oct 19 '15 at 08:44
  • 1
    console log your data part and show – VizardCrawler Oct 19 '15 at 08:44
  • 2
    Use the debugger built into your browser to set a breakpoint on your `if` and look at what's in `data` when you think the post is "empty." Also, note that `if data && data != "")` is functionally equivalent to `if (data)` (with those operands, if the left-hand operand is truthy, then the right-hand operand *cannot* be falsey and so you can just leave it off). – T.J. Crowder Oct 19 '15 at 08:45
  • @VishalKumarMitra: No need for stumbling about in the dark with a `console.log` torch when you can *turn the lights on*. :-) – T.J. Crowder Oct 19 '15 at 08:46
  • 1
    @T.J.Crowder : Thanks for the phrase. – VizardCrawler Oct 19 '15 at 08:52
  • I would hazard a guess that `data` isn't empty if your else isn't firing, as suggested, debug using your browser and break points, – llanato Oct 19 '15 at 09:11

1 Answers1

2

If the request succeeds and truly returns a blank string, your check would be fine, although you don't need the second half of it. E.g.:

if (data) {
    // Yes it's there
} else {
    // No, it's blank
}

...is sufficient. But if it may have any whitespace, you'll need to trim first:

if (data.trim()) {
    // Yes it's there
} else {
    // No, it's blank
}

(Some old browsers like IE8 may not have trim, but you can easily shim/polyfill it.)

To handle the error case, add a .fail handler; for cleanliness, let's move the success handler to .done as well, remove the declaration of the argument we're not using, and remove the unnecessary blank object.

That gives us:

$.post('blog.php')
    .done(function(data){
        if (data.trim())
        {
            $("#readmore").before(data);
        }
        else
        {
            alert('no more text');
        }
    })
    .fail(function() {
        // it failed
    });

Note that that assumes the Content-Type on the response is text/html or text/plain or similar, and so you'll get back a string. From what you've said, that's probably true.

You can always see exactly what's going on by using the debugger built into your browser:

  1. Open the developer tools using the menu or F12 (on IE and some others) or Ctrl+Shift+I (Chrome and some others, I think it's Cmd+Shift+I on Mac OSX).

  2. Switch to the Source tab (perhaps called Debugger on IE)

  3. Find your code, there's usually some way to browse the loaded scripts.

  4. Click in the left-hand gutter (next to the line number) of the first line in your done handler and a red mark or bar should appear, which means there's a breakpoint there that will stop your code. Do the same for the first line inside your fail handler.

  5. Trigger the post operation. One of your breakpoints should get hit, causing execution to stop at that point.

  6. If it's in the done handler, hover your mouse over data to see exactly what's in it (or switch to the "Console" tab, type data, and press enter, and it will show you the content of the data argument).

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks for the great answer. you'r right. I printed the content with exit(' ') and also return false for empty state in blog.php and now it works. – partiz Oct 19 '15 at 09:16