gpt4 book ai didi

javascript - echo json_encode($array) 传递了之前的 echo

转载 作者:行者123 更新时间:2023-12-03 04:19:00 30 4
gpt4 key购买 nike

我不了解 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 "echoed" by method of Connection in php)

["..."] (requested data)

最佳答案

如果您的 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);

关于javascript - echo json_encode($array) 传递了之前的 echo,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44026831/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com