AJAX:POST方法使用UTF-8方法、AJAX、POST、UTF

2023-09-10 14:57:59 作者:シ—欢喜而清净□

我试图发送数据超过阿贾克斯 UTF-8 ,但它改变 UNI code 。我会以两个短的例子解释一下:

I'm trying to send data as UTF-8 over Ajax, but it's changing some data in unicode. I'll explain it with two short examples:

一个简单的POST(不阿贾克斯)

A simple POST (without ajax)

<form accept-charset="UTF-8" method="POST" action="test2.php">
<input type="text" class="" name="text">
<input type="submit" class="button" value="Submit">
</form>

元和PHP的头总是设置:

Meta and PHP headers are always set:

<meta charset="utf-8">

header("Content-Type: text/html; charset=utf-8");

如果我提交阿拉伯字母(ب),并使用的strlen()将返回3.如果我用 mb_strlen()则返回1,这是所有的好,因为它应该是。

If I submit an Arabic letter (ب), and use strlen() it will return 3. If I use mb_strlen() it will return 1. This is all good as it should be.

现在阿贾克斯的版本。的形式,标头和元是相同的。但是的onsubmit()的JavaScript调用这个AJAX:

Now the Ajax version. The form, headers and meta are the same. But onsubmit() calls this ajax in Javascript:

... (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) { ... }

现在相同的测试给出了的strlen() 6和 mb_strlen()还6.ب是实际上转化为 6%u0628 某处Ajax的过程。而这不会发生与正常 POST (例如一个)。

Now the same test gives for strlen() 6 and for mb_strlen() also 6. The ب was actually converted to 6%u0628 somewhere in the Ajax process.. And this does not happen with a normal POST (example one).

什么我忘记/做错了在阿贾克斯的过程?

What am I forgetting/doing wrong in the Ajax process ?

推荐答案

我使用了很多AJAX,始终只使用UTF8和从未有过的一个问题,我主要是在Chrome和Safari的ios

i use alot ajax and always use only utf8 and never had a problem , i'm mostly on chrome and safari ios

或许可以通过删除所有使用新的headers.and改变乌尔AJAX脚本 FORMDATA (应该在最现代的浏览器)......当然不是全部。

maybe try changing ur ajax script by removing all the headers.and using the new FormData (should work on most modern browsers) ... sure not on all.

document.forms [0] 意味着需要在你的页面都是从第一个窗体域。 别人给它一个id,并调用的document.getElementById('MyForm的') 我也没有用了的readyState ... 的onload

document.forms[0] means it takes all the fields from first form in your page. else give it an id and call document.getElementById('myform') i also don't use anymore readyState... onload

返回你写在postresponsefunction响应 this.response

to return the response in postresponsefunction u write this.response

var fd=new FormData(document.forms[0]),
c=new XMLHttpRequest();
c.open('POST',strURL);
c.onload=postresponsefunction;
c.send(fd);

下面是一个COMPLET的PHP脚本后和放大器; AJAX文件名为test.php的

here is a complet working php script with post & ajax file is named 'test.php'

<?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");
  };
}
 
精彩推荐