Php AJAX:使用UTF-8的post方法

Php AJAX:使用UTF-8的post方法,php,javascript,ajax,utf-8,utf,Php,Javascript,Ajax,Utf 8,Utf,我试图通过Ajax以UTF-8的形式发送数据,但它改变了unicode中的一些数据。我将用两个简短的例子来解释: 一篇简单的文章(没有ajax) 现在同样的测试给出了strlen()6和mb_strlen()6。ب实际上在Ajax过程中的某个地方被转换为6%u0628。。正常的POST(示例一)不会发生这种情况 在Ajax过程中,我忘记了什么/做错了什么 我使用了很多ajax,总是只使用utf8,从来没有遇到过问题,我主要使用chrome和safari ios 也许可以尝试通过删除所有标题来更改

我试图通过Ajax以
UTF-8
的形式发送数据,但它改变了
unicode
中的一些数据。我将用两个简短的例子来解释:

一篇简单的文章(没有ajax)

现在同样的测试给出了
strlen()
6和
mb_strlen()
6。ب实际上在Ajax过程中的某个地方被转换为
6%u0628
。。正常的
POST
(示例一)不会发生这种情况


在Ajax过程中,我忘记了什么/做错了什么

我使用了很多ajax,总是只使用utf8,从来没有遇到过问题,我主要使用chrome和safari ios

也许可以尝试通过删除所有标题来更改您的ajax脚本。并使用新的
FormData
(应该适用于大多数现代浏览器)。。。当然不是

document.forms[0]
意味着它将获取页面中第一个表单的所有字段。 否则给它一个id并调用
document.getElementById('myform')
我也不再使用
readyState
<代码>加载

要在postresponsefunction u中返回响应,请写入
this.response

var fd=new FormData(document.forms[0]),
c=new XMLHttpRequest();
c.open('POST',strURL);
c.onload=postresponsefunction;
c.send(fd);
下面是一个完整的php脚本,带有名为“test.php”的post&ajax文件

<?php
if($_REQUEST){
print_r($_REQUEST);
}else{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>postajax</title>
<script>
var 
rf=function(){
 document.getElementsByTagName('div')[0].innerText=this.response;
},
sf=function(e){
 e.preventDefault();e.stopPropagation();
 var fd=new FormData(document.getElementsByTagName('form')[0]),
 c=new XMLHttpRequest();
 c.open('POST','test.php');
 c.onload=rf;
 c.send(fd);
};
window.onload=function(){
 document.getElementsByTagName('form')[0].onsubmit=sf;
}
</script>
</head>
<body>
<form>
<input type="text" name="mytext"><input type="submit" value="submit">
</form><div></div>
</body>
</html>
<?php
}
?>

你的剧本似乎很好用。谢谢!你知道FormData在哪些浏览器上不起作用吗?我想使用它,但也需要它在大多数浏览器(和旧浏览器)上工作。是的,现代浏览器不支持这个旧浏览器。这是XHR2和FormData,这是其他浏览器的FormData的polyfill。如何处理旧浏览器?如果某些东西不起作用,你需要找到等效的polyfill。在你的例子中,FormData的polyfill。。。xhr现在可以在90%的计算机上运行。也可以在最新的ie上运行。我会告诉旧的浏览器下载一个新的浏览器。我们需要一个标准。这将是新的标准。保持简单。
... (initiating HttpReq)
self.xmlHttpReq.open('POST', strURL, true);
self.xmlHttpReq.setRequestHeader("charset", "utf-8");
self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); 
...
if (self.xmlHttpReq.readyState == 4) { ... }
var fd=new FormData(document.forms[0]),
c=new XMLHttpRequest();
c.open('POST',strURL);
c.onload=postresponsefunction;
c.send(fd);
<?php
if($_REQUEST){
print_r($_REQUEST);
}else{
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>postajax</title>
<script>
var 
rf=function(){
 document.getElementsByTagName('div')[0].innerText=this.response;
},
sf=function(e){
 e.preventDefault();e.stopPropagation();
 var fd=new FormData(document.getElementsByTagName('form')[0]),
 c=new XMLHttpRequest();
 c.open('POST','test.php');
 c.onload=rf;
 c.send(fd);
};
window.onload=function(){
 document.getElementsByTagName('form')[0].onsubmit=sf;
}
</script>
</head>
<body>
<form>
<input type="text" name="mytext"><input type="submit" value="submit">
</form><div></div>
</body>
</html>
<?php
}
?>
if (!window.XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    return new ActiveXObject(”Microsoft.XMLHTTP”);
  };
}