3

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

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

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

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

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

文件 3:loadTest.html,使用 Notepad++ 以 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。

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

???带有加载方法的jQuery Ajax。

怎么了?请帮忙。

4

1 回答 1

2

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

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

另外看看下面的 URL 是否可以提供额外的帮助:http ://www.phpwact.org/php/i18n/utf-8

于 2012-06-17T16:45:19.733 回答