简体   繁体   English

.php文件的字符编码错误

[英]Character encoding error for .php file

Have made a route for MarkersController.php which returns json, but when i navigate to the route I get the following error: 为MarkersController.php做了一个返回json的路由,但是当我导航到路由时,我收到以下错误:

The character encoding of the HTML document was not declared. 未声明HTML文档的字符编码。 The document will render with garbled text in some browser configurations if the document contains characters from outside the US-ASCII range. 如果文档包含US-ASCII范围之外的字符,则文档将在某些浏览器配置中使用乱码文本进行渲染。 The character encoding of the page must be declared in the document or in the transfer protocol. 必须在文档或传输协议中声明页面的字符编码。

My route is as follows: 我的路线如下:

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

MarkersController.php 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>

Do you have Content-Type definition into your head.html? 你的head.html中有内容类型定义吗? If the answer is no, try to put 如果答案是否定的,请尝试放置

<meta http-equiv="Content-Type" content="text/html;charset=[your-charset]" />

into your head.html 进入你的head.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM