如何通过jQuery $.load()实用程序函数获取PHP将utf-8字符串传递给JavaScript


How to get PHP pass utf-8 string to JavaScript through jQuery $.load() utility function?

我使用 jQuery $.load() 实用程序函数从 PHP 获取 utf-8 字符串时遇到了一些问题。

文件 1:myrecord.txt,使用记事本++ 以 utf-8 编码保存

<p>你好, jQuery Ajax with load method.</p>.

文件 2:myrecord.php,使用记事本++ 以 utf-8 编码保存

<?php
  echo '<p>你好, jQuery Ajax with load method.</p>';
?>

文件 3:loadTest.html,使用记事本++以utf-8编码保存

<html>
<head>
   <title>Load Example</title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   <script type="text/javascript"           
        src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js">
   </script>
   <script type="text/javascript" language="javascript">
        $(document).ready(function() {
    $('div').load('myrecord.txt');
    });
   </script>
</head>
<body>
      <div></div>
</body>
</html>

如果我按原样运行 loadTest.html我得到的结果正确显示为

你好, jQuery Ajax with load method.

但是,如果我将文件 3 中的加载方法更改为 $('div').load('myrecord.php'),则显示将更改为

???jQuery Ajax with load method.

怎么了?请帮忙。

您可以尝试使用readfile函数使用php文件加载该文件,并且该函数应该具有UTF-8输出的标头吗?

header ('Content-type: text/html; charset=utf-8');
readfile('myrecord.txt');

此外,请查看以下 URL 是否可以提供其他帮助:http://www.phpwact.org/php/i18n/utf-8