我不知道 jQuery,所以如果这是一个愚蠢的问题,我很抱歉。
当我将值从 php 传递给 javascript 时,即使之前用 echo 打印的消息也被传递了,我怎么能避免这种情况?
php
$Connection = new Connection("...");
$array = $Connection->arrCol("SELECT IT FROM global", "IT");
echo json_encode($array);
jQuery
$(document).ready(function(){
$("button").click(function(){
$.get("services/test.php", function(data, status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
结果
CONNECTED at (localhost) (user: root) (db: ...) stausUptime: 440790 Threads: 1 Questions: 1244 Slow queries: 0 Opens: 53 Flush tables: 1 Open tables: 1 Queries per second avg: 0.2 (message "回显”通过 php 中的连接方法)
["..."](请求的数据)
如果您的Connection
类执行回显并且您不希望将其混入您的 JSON 输出,则您将必须控制输出缓冲区。
例如:
// Start catching the output
ob_start();
// Run code that echoes unwanted stuff under the hood
$Connection = new Connection("...");
$array = $Connection->arrCol("SELECT IT FROM global", "IT");
// Clear the output buffer and stop buffering (i.e. discard that echo)
ob_end_clean();
// Output JSON
header('Content-Type: application/json');
echo json_encode($array);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句