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

[HELP] 关于跨域ajax~post的问题

[复制链接]
发表于 2012-6-4 11:26:18 | 显示全部楼层 |阅读模式
我最近在用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数据.

附测试代码:
HTML复制代码
 
      <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~ 方便查看.

PHP复制代码
 
$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是用来简单分表的.

额~ 貌似数据库处理那部分还有些问题... - -难道不是这么用的么.
发表于 2012-6-4 11:53:02 | 显示全部楼层
楼主再说得详细一点,报什么错呢。
 楼主| 发表于 2012-6-4 12:01:19 | 显示全部楼层
justdoit 发表于 2012-6-4 11:53
楼主再说得详细一点,报什么错呢。

没有报错~  - -
如果是跨域就是整个貌似都不执行~连400或者403错误都不提示. 就是在firebug里面的console查看~ 毫无任何js的post动作,但是在本域下却可以提交.只是不插入数据库.
发表于 2012-6-4 13:11:33 | 显示全部楼层
ajax不能跨域,鉴定完了。
 楼主| 发表于 2012-6-4 13:27:31 | 显示全部楼层
yunnysunny 发表于 2012-6-4 13:11
ajax不能跨域,鉴定完了。

哦对~ 悲剧~
发表于 2012-6-5 09:25:57 | 显示全部楼层
楼主可以用最新版jquery的jsonp方法试一下,这个可以跨域的。
发表于 2012-6-5 12:49:00 | 显示全部楼层
POST方式是不可以的
建议使用jQuery的getJSON方法
发表于 2012-6-6 14:42:04 | 显示全部楼层
XHR不能跨域,可以用JSONP。
如果不打算兼容较旧的浏览器,还可以考虑postMessage或XHR level2
 楼主| 发表于 2012-6-8 10:55:42 | 显示全部楼层
{:soso_e113:} 用了~ 一提醒就发现了~ 悲剧啊 - -
发表于 2012-10-8 21:57:33 | 显示全部楼层
可以在php的header中设置个允许某域访问的。

本版积分规则