具体问题:
从php脚本向JavaScript函数发送数据
echo json_encode($rows);
当页面加载时,我正在运行php脚本,然后输出显示在页面上,这实际上是我不想显示的。我试图用ob_end_clean()隐藏echo,但这似乎破坏了一切。
解决办法:
可以像这样检查ajax请求
/* AJAX check */ if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') { /* your ajax here code will go here */ header('Content-type: application/json'); echo json_encode($rows); exit(); } //non ajax code ... ...
echo只有在它是一个AJAX调用时才会运行