.php文件的字符编码错误

fst*_*104 5 html php json slim

为MarkersController.php做了一个返回json的路由,但是当我导航到路由时,我收到以下错误:

未声明HTML文档的字符编码.如果文档包含US-ASCII范围之外的字符,则文档将在某些浏览器配置中使用乱码文本进行渲染.必须在文档或传输协议中声明页面的字符编码.

我的路线如下:

$app->get('/markers/?', function () use ($app) {
    $controller = new UF\MarkersController($app);
    return $controller->getMarkersJSON();
}); 
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Ste*_* P. 0

你的 head.html 中有 Content-Type 定义吗?如果答案是否定的,请尝试将

<meta http-equiv="Content-Type" content="text/html;charset=[your-charset]" />
Run Code Online (Sandbox Code Playgroud)

进入你的 head.html