简体   繁体   English

为什么在$ .post()回调中收到空响应?

[英]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" do.php成功后,回显“成功”

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: 在HTML中,将有很多帖子,其各自的ID位于div中,由数据库中的php放置,如​​下所示:

<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. 在php中,现在什么也没有...它只是打印成功...我就这样进行测试。

echo "success";

When going to the linking directly, it printing "success". 直接转到链接时,它会打印“成功”。 Is 'Data' a string? “数据”是字符串吗? I mean is this correct? 我的意思是这样吗?

if (data=='success')

您是否正在打印/回显do.php文件中的数据值?

It certainly looks as though jQuery is running into an AJAX error. 看起来jQuery似乎遇到了AJAX错误。 If jQuery hits an error using one of those shorthand methods ($.get() $.set()) it fails silently. 如果jQuery使用这些速记方法之一($ .get()$ .set())遇到错误,则它会静默失败。 However, if you implement the ajaxError() method in jQuery, you can get at the error. 但是,如果在jQuery中实现ajaxError()方法,则可能会遇到错误。 I would suggest doing this before going any further. 我建议您先进行此操作。

您是否检查过Web服务器错误日志,以确保服务器没有出现静默故障并记录错误而不是显示错误?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM