zoukankan      html  css  js  c++  java
  • PHP+JQuery实现ajax跨域

    jQuery实现ajax跨域
    1、dataType:'jsonp'
    2、type: 'get'
    3、把要传的参数以url方式传出去  url:'http://gameapi.feiliu.com/lqzg?tel='+telnumber,

    JS代码

    <script>
    $.ajax({
                                    url:'http://gameapi.feiliu.com/lqzg?tel='+telnumber,
                                    async: true, 
                                    type:"get",
                                    dataType:'jsonp',
                                    jsonp:"jsoncallback",
                                    data:{telnumber:telnumber},
                                    success:function(data,status){
                                        if(data.key == 1){
                                            $('#gacode').text('验证码为:'+data.mes).css('display', 'block');
                                        }else if(data.key == -1){
                                            $('#gacode').text('出错了!'+data.mes).css('display', 'block');
                                        }else{
                                            alert('没有任何东西!');
                                        }
                                    },
                                    error:function(){
                                         alert(arguments[1]);
                                    }                                
                                });
    </script>

    php代码(CI框架)

    public function index()
            {    
                header('Content-Type:text/html;Charset=utf-8');  
                $tel = $_GET['telnumber'];
                if(! preg_match("/1[3458]{1}d{9}$/",$tel)){
                    $key = -1;
                    $mes = '手机号无效!';
                }else{
                    //检查号码是否第一次领取
                    if( $this->gamem->test_phone($tel)){
                        $key = -1;
                        $mes = '手机号已经领取过!';
                        
                    }else{
                        //随机选取一个code
                        $code = $this->gamem->get_code();            
                        if($code){
                            //将该用户信息插入
                            $ret = $this->gamem->insert_user($tel);
                            if($ret){
                                $key = 1;
                                $mes = $code;
                            }
                        }
                    }
                }  
                $data = array(
                    'key' => $key,
                    'mes' => $mes
                );
                echo $_GET['jsoncallback'] . "(".json_encode($data).")";  
            
                exit;
                    
            }    
  • 相关阅读:
    使用ViewPager实现三个fragment切换
    handler
    Android 源码解析之AsyncTask
    android的生命周期
    安卓在SQLiteOpenHelper类进行版本升级和降级
    安卓ListView操作的两种方法
    表格布局TableLayout
    线性布局和相对布局
    遇到tomcat端口被占用问题解决方案
    easyUI笔记09.03
  • 原文地址:https://www.cnblogs.com/hejun695/p/5231666.html
Copyright © 2011-2022 走看看