用户
 找回密码
 入住 CI 中国社区
搜索
查看: 8670|回复: 7
收起左侧

[AJAX] CI中AJAX的POST和FORM的POST有区别?

[复制链接]
发表于 2010-6-26 23:41:59 | 显示全部楼层 |阅读模式
上次我说过用提交%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()" />&nbsp;&nbsp;
    <input type="button" value="http_get" onclick="http_get()">&nbsp;&nbsp;
    <input type="button" value="http_get" onclick="ajax_get()">
  </body>
</html>
 
复制代码


没用CI框架的情况下

PHP复制代码
<?php
  print_r($_GET);
?>
复制代码

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>
复制代码
发表于 2010-6-27 00:43:21 | 显示全部楼层
首先,post 不分 ajax 和 普通,如果浏览器不给特殊标志,服务器是区分不出来的。
这个毫无疑问是 ci 过滤了某些东西,需要查看 ci 源码才能确定。
 楼主| 发表于 2010-6-27 09:36:08 | 显示全部楼层
首先,post 不分 ajax 和 普通,如果浏览器不给特殊标志,服务器是区分不出来的。
这个毫无疑问是 ci 过滤 ...
Hex 发表于 2010-6-27 00:43

CI过滤这些是出于什么目的呢??
如果我拓展后把这些过滤都去掉,会不会导致其他的什么问题呢?(但PHP并没过滤这些啊...)
 楼主| 发表于 2010-6-27 09:38:56 | 显示全部楼层
对了...就连URI中出现%25AC都会被过滤,那...CI到底是在哪一层就开始进行过滤了啊...
发表于 2010-6-27 10:52:47 | 显示全部楼层
过滤,用意很明显就是为了安全,否则不会有“过滤”这种东西存在,呵呵。
PHP 本身不安全,如果你不过滤的话。(SQL 注入,XSS 攻击等等)

至于怎么发现是哪里过滤的,很简单,跟踪。
也可以看看本论坛的源码分析文章。
发表于 2010-9-16 12:06:15 | 显示全部楼层
用POST方式 PHP脚本里就用POST数组接收
不管是AJAX来的还是你FORM METHOD里面写的POST。
发表于 2010-9-19 16:16:09 | 显示全部楼层
AJAX的POST和FORM的POST个人觉得只是提交方式改变了,对php来说都无所谓吧.
发表于 2010-9-28 06:42:48 | 显示全部楼层
是PHP接受端用发的区别。
POST过来用$_POST数组接,GET过来用$_GET接

本版积分规则