开发者

Why am I getting an empty response in my $.post() callback?

What's wrong with this code? I'm not able to validate 'data':

$.post('http://localhost/do.php', function(data) 
{
  if (开发者_开发问答data == "success")
  {
    //do something... but this line never hit!
  }
});

I also tried,

alert(data);

but got an empty alert box!

the do.php on success, echo's "success"

echo "success";

ok.. here is the complete original code:

<script type="text/javascript">
function confirm_delete(id)
{
    var r=confirm("Are you sure you want to delete?");
    if (r==true)
    {
      var path = "http://localhost/site/index.php/delete/" + id;

     $.post(path, function(data) 
      {
            if (data=='success')
            {
            $('#'+id).remove();
            }
            else 
            {
            alert("Unable to delete, try again!");
            }
      });

    }else
    {
      //cancel
    }
}

//-->
</script>

In the HTML, there will be many posts with their respective id's in div, laid by php from database, somewhat like this:

<div id="1">
<div class='post'>Something</div>
<a href="#"><img src="styles/plugins/buttons/icons/cross.png" height="8" width="8" title="Remove" onclick="confirm_delete(1)"/></a>
</div>

<div id="2">
<div class='post'>Something</div>
<a href="#"><img src="styles/plugins/buttons/icons/cross.png" height="8" width="8" title="Remove" onclick="confirm_delete(2)"/></a>
</div>

<div id="3">
<div class='post'>Something</div>
<a href="#"><img src="styles/plugins/buttons/icons/cross.png" height="8" width="8" title="Remove" onclick="confirm_delete(3)"/></a>
</div>

In the php there is nothing now... it just prints success... i made it like that for testing.

echo "success";

When going to the linking directly, it printing "success". Is 'Data' a string? I mean is this correct?

if (data=='success')


It certainly looks as though jQuery is running into an AJAX error. If jQuery hits an error using one of those shorthand methods ($.get() $.set()) it fails silently. However, if you implement the ajaxError() method in jQuery, you can get at the error. I would suggest doing this before going any further.


are you printing/echo-ing the value of data in your do.php file ?


Have you checked your web servers error log to make sure the server is not silently failing and logging the error instead of displaying it?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜