php 如何通过ajax获取UTF-8格式的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19898977/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get UTF-8 format data through ajax
提问by Let me see
Sorry if i am asking a silly question but i really need a solution for it.I am requesting for some data using ajax and the script is
对不起,如果我问了一个愚蠢的问题,但我真的需要一个解决方案。我正在使用 ajax 请求一些数据,脚本是
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
$url='http://localhost/path/to/the/php/script';
xmlhttp.open("GET",$url,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
and this is my php script
这是我的 php 脚本
<?php
$sqlurl='/path/to/my/file';
if(file_exists($sqlurl))
{
$sqlitedata= file_get_contents($sqlurl);
echo $sqlitedata;
}
else {
echo 'the file is not available right now';
}
?>
Now the problem is that the data in my file is in the UTF-8 formatbut when i try to get it through ajax then what i get is a series of question marks (??????). How can i request for the data through ajax in the same format in which it originally exists.
现在的问题是我文件中的数据是UTF-8 格式,但是当我尝试通过 ajax 获取它时,我得到的是一系列问号 (??????)。我如何通过 ajax 以原始存在的相同格式请求数据。
回答by mikakun
asuming your file is indeed a xml file, assuming the page making the request is utf8,
假设您的文件确实是一个xml文件,假设发出请求的页面是utf8,
then before you echo
anything in your php file :
然后在你echo
的 php 文件中的任何内容之前:
<?php header("Content-Type: application/xml; charset=utf-8"); ?>
for extra safety in your xml :
为了您的 xml 更加安全:
<?xml version="1.0" encoding="UTF-8"?>
edit you can do that as well :
编辑你也可以这样做:
header('Content-type: text/xml');
with
和
<?xml version="1.0" encoding="UTF-8"?>
回答by Rohan Kumar
Try to use utf8-encode()like,
尝试使用utf8-encode() 之类的,
echo utf8_encode($sqlitedata);
If you are using jquery
then use $.ajax()with contentType option
which is default
like,
如果您正在使用jquery
,然后使用$阿贾克斯()与contentType option
这default
类似,
function loadXMLDoc()
{
$.ajax({
type:"GET",
url:"http://localhost/path/to/the/php/script",
contentType: "application/x-www-form-urlencoded;charset=utf-8",
success: function(data){
$("#myDiv").html(data);
}
});
}
回答by Zahidul Hossein Ripon
between head tag place this
在头标签之间放置这个
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
If you use any query in your php page place this before your query
如果您在 php 页面中使用任何查询,请将其放在查询之前
mysql_query('SET CHARACTER SET utf8');
$result1 = mysql_query("SET NAMES utf8");