IvanCI 发表于 2012-6-4 11:26:18

关于跨域ajax~post的问题

我最近在用CI做一个项目,
项目开放一个简单的类似api的玩意儿,

我需要从站外,也就是别的域名或者网站进行ajax来进行post数据,
但是我这边程序接收不到数据.

例:
域名A 是我的项目域名,没有一个 linkA: " A/index.php/point/points "的方法.
域名B 是我的测试域名, 有一个简单html来测试ajax , 来进行对 linkA post数据.

我用firebug查看,应该是B域名下看不到有post信息.
但是我在域名A下用同样的html进行测试确可以看到post信息,以及能够接受post数据.

我搜索了下论坛,大部分人是害怕跨域攻击- -
但是这个没办法,我需要进行这样的验证.
当然安全那部分是稍后的处理,会有一系列的验证.理论上应该还是安全的.

求解如何解决这个跨域ajax post数据.

附测试代码:

      <a href="javascript:void(0)">POST</a>
      <script>
            function test_post(){
                var local_url =window.location.href;
                var ref = document.referrer;
                $.ajax({
                  url:'http://eggs/index.php/point/points',
                  type:'POST',
                  data:{'a':199.10,'b':100.87,'url':local_url,'ref':ref},
                  dataType:'text',
                  success:function(data){
                        alert(data);
                  }
                });
            }


            jQuery(document).ready(function($){

            });
      </script>


a 标签为了能够手动点击post~ 方便查看.


$this->point_x = $this->input->post('a');
      $this->point_y = $this->input->post('b');
      $this->url = $this->input->post('url');
      $this->user_sign = $this->input->post('user_sign');
      $this->ip =$this->input->ip_address();
      $this->user_agent = $this->input->user_agent();
      $this->referrer = $this->input->post('ref');

      $point_table = 'test_'.table_choose($this->url);

      $data = array(
            'uid'=>0,
            'url_id'=>0,
            'point_x'=>$this->point_x,
            'point_y'=>$this->point_y,
            'browers'=>$this->user_agent,
            'source'=>$this->referrer
      );
      $this->db->insert_string($point_table,$data);



这是处理function, point_table是用来简单分表的.

额~ 貌似数据库处理那部分还有些问题... - -难道不是这么用的么.

justdoit 发表于 2012-6-4 11:53:02

楼主再说得详细一点,报什么错呢。

IvanCI 发表于 2012-6-4 12:01:19

justdoit 发表于 2012-6-4 11:53 static/image/common/back.gif
楼主再说得详细一点,报什么错呢。

没有报错~- -
如果是跨域就是整个貌似都不执行~连400或者403错误都不提示. 就是在firebug里面的console查看~ 毫无任何js的post动作,但是在本域下却可以提交.只是不插入数据库.

yunnysunny 发表于 2012-6-4 13:11:33

ajax不能跨域,鉴定完了。

IvanCI 发表于 2012-6-4 13:27:31

yunnysunny 发表于 2012-6-4 13:11 static/image/common/back.gif
ajax不能跨域,鉴定完了。

哦对~ 悲剧~ :Q

justdoit 发表于 2012-6-5 09:25:57

楼主可以用最新版jquery的jsonp方法试一下,这个可以跨域的。

kissgxd 发表于 2012-6-5 12:49:00

POST方式是不可以的
建议使用jQuery的getJSON方法

myst729 发表于 2012-6-6 14:42:04

XHR不能跨域,可以用JSONP。
如果不打算兼容较旧的浏览器,还可以考虑postMessage或XHR level2

IvanCI 发表于 2012-6-8 10:55:42

{:soso_e113:} 用了~ 一提醒就发现了~ 悲剧啊 - -

ゞ放飛@夢想. 发表于 2012-10-8 21:57:33

可以在php的header中设置个允许某域访问的。
页: [1]
查看完整版本: 关于跨域ajax~post的问题