|
上次我说过用提交%AC之类的数据会出现错误
后来通过在前台JS中将字符串数据先进行URI编码
encodeURIComponent(obj.value)
通过这样的方法,在后台就可以正常显示%AC了
但是........今天在测试AJAX的时候,发现AJAX使用POST提交带%AC的字符串,依然是错误,进行了encodeURIComponent编码也一样...
例子在下面,难道CI中,AJAX的POST提交和FORM的POST提交有区别??
BTW,%AC如果出现在URI中也不行(经过encodeURIComponent为 %25AC了)...依然是错误...
测试过,在没用CI框架的情况下,URI中是可以出现%25AC的(如果没通过encodeURIComponent编码的话,也会出现空白)
CONTROLLERS
PHP复制代码 <?php
class Test extends Controller {
function Test () {
parent ::Controller();
}
function index () {
$this->load->view('test');
}
}
function http_post () {
$data['str_request'] = $this->input->post('_str');
$this->load->view('test',$data);
}
function ajax_post () {
echo $this->input->post('_str');
}
function http_get($str) {
$data['str_request'] = $str;
$this->load->view('test',$data);
}
function ajax_get ($str) {
echo $str;
}
}
?> 复制代码
VIEW
HTML复制代码 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8" />
<title>TEST_POST </title>
<script type="text/javascript" language="javascript">
// <![CDATA[
var base_url = "<?php echo base_url(); ?>";
var xmlHttp
function GetXmlHttpObject() {
var xmlHttp = null;
try {
//Firefox, Opera 8.0+, Safari
xmlHttp = new XMLHttpRequest();
} catch (e) {
//Internet Explorer
try {
xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
}
return xmlHttp;
}
function ajax_post() {
xmlHttp = GetXmlHttpObject();
if (xmlHttp == null) {
alert ("浏览器不支持AJAX技术");
return;
}
//var timeRandom = Math.random()*100000000000000000;
var url = base_url+"test/ajax_post";
var post_data = encodeURIComponent(document.getElementById('_str').value);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
document.getElementById('_request').innerHTML = xmlHttp.responseText;
}
};
xmlHttp.open("POST",url,true);
xmlHttp.setRequestHeader("Content-Length", post_data.length);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send("_str=" + post_data);
}
function ajax_get() {
xmlHttp = GetXmlHttpObject();
if (xmlHttp == null) {
alert ("浏览器不支持AJAX技术");
return;
}
var url = base_url+"test/ajax_get/"+encodeURIComponent(document.getElementById('_str').value);
xmlHttp.onreadystatechange = function(){
if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
document.getElementById('_request').innerHTML = xmlHttp.responseText;
}
};
xmlHttp.open("GET",url,true);
xmlHttp.setRequestHeader("Cache-Control", "no-cache");
xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlHttp.send(null);
}
function http_post(_form) {
document.getElementById('_str').value = encodeURIComponent(document.getElementById('_str').value);
_form.submit();
}
function http_get() {
var url = base_url+"test/http_get/"+encodeURIComponent(document.getElementById('_str').value);
window.location.href = url;
}
// ]]>
</script>
</head>
<body>
<div id="_request" style="border: 2px solid #555; width:300px; height:100px;">
<?php echo isset($str_request)?($str_request):""; ?>
</div>
<form action="<?php echo site_url(array('test','http_post')); ?>" method="POST">
String: <input type="text" id="_str" name="_str" value="" />
<br /><br />
<input type="button" value="http_post" onclick="http_post(this.form)" />
</from>
<input type="button" value="ajax_post" onclick="ajax_post()" />
<input type="button" value="http_get" onclick="http_get()">
<input type="button" value="http_get" onclick="ajax_get()">
</body>
</html>
复制代码
没用CI框架的情况下
HTML复制代码 <html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<input type="text" id="ttt" name="ttt" value="" />
<input type="button" value="click" onclick="http_get()" />
<script type="text/javascript" language="javascript">
function http_get() {
var t = encodeURIComponent(document.getElementById('ttt').value);
window.location.href='test.php?ttt='+t;
}
</script>
</body>
</html> 复制代码 |
|