抱歉,如果我问了一个愚蠢的问题,但我确实需要一个解决方案。我正在使用 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>
这是我的 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';
}
?>
现在的问题是我的文件中的数据位于UTF-8 格式但是当我尝试通过 ajax 获取它时,我得到的是一系列问号 (??????)。我怎样才能通过ajax以与最初存在的格式相同的格式请求数据。
假设你的文件确实是一个xml文件,假设发出请求的页面是utf8,
然后在你面前echo
php 文件中的任何内容:
<?php header("Content-Type: application/xml; charset=utf-8"); ?>
为了让您的 xml 更加安全:
<?xml version="1.0" encoding="UTF-8"?>
编辑你也可以这样做:
header('Content-type: text/xml');
with
<?xml version="1.0" encoding="UTF-8"?>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)