0
我有一個項目的問題。 我有一臺服務器上像這樣的PHP腳本:如何將json格式從sql-multi-query php-script轉換爲javascript?
$mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (!$mysqli->query("DROP TABLE IF EXISTS test") || !$mysqli->query("CREATE TABLE test(id INT)")) {
echo "Table creation failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
$sql = "SELECT COUNT(*) as f FROM R;";
$sql.= "SELECT COUNT(*) as fo FROM Ro;";
$sql.= "SELECT P, U, D FROM Profile where U = 'lol';";
$sql.= "SELECT IU FROM I WHERE UID = '2';";
if (!$mysqli->multi_query($sql)) {
echo "Multi query failed: (" . $mysqli->errno . ") " . $mysqli->error;
}
do {
if ($res = $mysqli->store_result()) {
echo json_encode($res->fetch_all(MYSQLI_ASSOC));
$res->free();
}
} while ($mysqli->more_results() && $mysqli->next_result());
我得到這樣的迴應:
[{"f":"0"}][{"fo":"0"}][{"P":"0",U":"lol","D":"xD"}]
[{"I":"lol"},{"I":"lolol"}]
但是當我通過javascript一樣要求這樣的回覆:
$.post("http://localhost/lol/lol.php", {}, function(data){
console.log(data);
}, "json");
我收到空。 但是,當我使用沒有「json」的post.request時,我得到一個響應,但不是以json格式。 我該怎麼做? 非常感謝您的幫助! 問候 菲利克斯