为返回 json 的 MarkersController.php 创建了一个路由,但是当我导航到该路由时,出现以下错误:
未声明 HTML 文档的字符编码。如果文档包含 US-ASCII 范围之外的字符,则在某些浏览器配置中,文档将呈现为乱码文本。页面的字符编码必须在文档或传输协议(protocol)中声明。
我的路线如下:
$app->get('/markers/?', function () use ($app) {
$controller = new UF\MarkersController($app);
return $controller->getMarkersJSON();
});
MarkersController.php
<!DOCTYPE html>
<html lang="en">
<head>
{% include 'components/head.html' %}
</head>
<body>
<?php
include('DB_INFO.php');
function getMarkersJSON(){
// Opens a connection to a MySQL server.
$connection = mysqli_connect($server, $username, $password);
if (!$connection) {
die('Not connected : ' . mysqli_error());}
// Sets the active MySQL database.
$db_selected = mysqli_select_db($database, $connection);
if (!$db_selected) {
die('Can\'t use db : ' . mysqli_error());}
// Selects all the rows in the markers table.
$query = "SELECT * FROM tester WHERE 1";
$result = mysqli_query($connection, $query);
if (!$result) {
die('Invalid query: '. mysqli_error());
}
$markers = array();
while ($row = mysqli_fetch_assoc($result)) {
//Assuming "lat" is column name in tester table. Please change it if required.
$lat= $rows['lat'];
//Assuming "lng" is column name in tester table. Please change it if required.
$lng= $rows['lng'];
$markers = array('lat' => $lat, 'lng' => $lng);
}
echo json_encode($markers);
}
?>
</body>
</html>
最佳答案
您的 head.html 中是否有 Content-Type 定义? 如果答案是否定的,尝试把
<meta http-equiv="Content-Type" content="text/html;charset=[your-charset]" />
进入你的 head.html
关于php - .php 文件的字符编码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32207755/