Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I have some problem to get the utf-8 string from PHP using jQuery $.load() utility function.

File 1: myrecord.txt, saved in utf-8 encoding using Notepad++

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

File 2: myrecord.php, saved in utf-8 encoding using Notepad++

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

File 3: loadTest.html, saved in utf-8 encoding using Notepad++

<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>

If I run loadTest.html as is, the result I got is correctly displayed as

你好, jQuery Ajax with load method.

However if I change the load method in file 3 to $('div').load('myrecord.php'), then the display changes to

???jQuery Ajax with load method.

What's wrong? Please help.

like image 917
user1462053 Avatar asked Dec 11 '25 20:12

user1462053


1 Answers

can you try loading that file using php file by using readfile function and that function should have header for utf-8 output?

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

In addition see if following URL can be of additional help: http://www.phpwact.org/php/i18n/utf-8

like image 168
deej Avatar answered Dec 14 '25 08:12

deej