技术头条 - 一个快速在微博传播文章的方式     搜索本站
您现在的位置首页 --> JavaScript --> 用于ajax跨域提交post或者get请求的代理程序

用于ajax跨域提交post或者get请求的代理程序

浏览:2276次  出处信息

局限性:1.服务器必须配置有cURL 2.增加一次服务器的请求代码如下:


/**
 * AJAX代理程序,用于跨域提交请求
 * 用于发送post或get请求
 * 只能被ajax请求所访问,直接访问将被忽略
 * 
 * @author 废墟 <r.anerg@gmail.com>
 * @link http://anerg.com
 */
ini_set("display_error"0
);
error_reporting(0
);
date_default_timezone_set('Asia/Shanghai'
);
define('BASEPATH'dirname(__FILE__
));

class 
ajaxproxy 
{

    public function 
__construct
() {
        
ob_start
();
        
header("Expires: -1"
);
        
header("Cache-Control: no-store, private, post-check=0, pre-check=0, max-age=0"FALSE
);
        
header("Pragma: no-cache"
);
        
header("Content-type: text/html; charset=utf-8"
);
        if (
get_magic_quotes_gpc
()) {
            
$in = array(& $_GET, & $_POST, & $_COOKIE, & $_REQUEST
);
            while (list(
$k$v) = each($in
)) {
                foreach (
$v as $key => $val
) {
                    if (!
is_array($val
)) {
                        
$in[$k][$key] = stripslashes($val
);
                        continue;
                    }
                    
$in[] = & $in[$k][$key
];
                }
            }
            unset(
$in
);
        }
    }

    public function 
run
() {
        if (
$_SERVER['HTTP_X_REQUESTED_WITH'] === 'XMLHttpRequest'
) {
            
$request_url $this->build_url
();
            if (
$request_url !== FALSE
) {
                
$rs $this->_curl($request_url$_POST
);
                if (
$rs === FALSE
) {
                    
$this->log($request_url'AJAX_PROXY'
);
                    
$out = array('error' => -2000'msg' => '系统连接超时!'
);
                    echo 
json_encode($out
);
                } else {
                    echo 
$rs
;
                }
            }
        }
    }

    private function 
build_url
() {
        
$app trim($_GET['app'
]);
        
$act trim($_GET['act'
]);
        if (empty(
$app) || empty($act
)) {
            return 
FALSE
;
        } else {
            
$args $_GET
;
            unset(
$args['app'], $args['act'
]);
            
$uri ''
;
            if (!empty(
$args
)) {
                
$uri .= '?'
;
                foreach (
$args as $k => $v
) {
                    
$tmp[] = $k '=' $v
;
                }
                
$uri .= join('&'$tmp
);
            }
            
$request_url 'http://' $app '.xs8.cn/ajax/' $act $uri
;
            return 
$request_url
;
        }
    }

    private function 
_curl($url$post_data = array(), $second 5
) {
        
$ch curl_init
();
        
curl_setopt($chCURLOPT_URL$url
);
        
curl_setopt($chCURLOPT_HEADER0
);
        
curl_setopt($chCURLOPT_TIMEOUT$second
);
        
curl_setopt($chCURLOPT_COOKIE$_SERVER['HTTP_COOKIE'
]);
        if (!empty(
$post_data
)) {
            
curl_setopt($chCURLOPT_POST1
);
            
curl_setopt($chCURLOPT_POSTFIELDShttp_build_query($post_data
));
        }
        
curl_setopt($chCURLOPT_RETURNTRANSFERtrue
);
        
$content curl_exec($ch
);
        
curl_close($ch
);
        return 
$content
;
    }

    private function 
log($log_data$log_file
) {
        
$data date("Y-m-d H:i:s") . " --> " trim($log_data) . "n"
;
        
$file dirname(BASEPATH) . '/application/logs/' $log_file '_' date("Y-m-d"
);
        
error_log($data3$file
);
    }

}

$obj = new ajaxproxy
();
$obj->run
();
?>

建议继续学习:

  1. 优雅绝妙的Javascript跨域问题解决方案    (阅读:6689)
  2. jQuery中getJSON跨域原理详解    (阅读:5549)
  3. 跨域请求的iframe解决方案(1)    (阅读:5319)
  4. ajax-cross-domain    (阅读:4927)
  5. 利用跨域资源共享(CORS)实现ajax跨域调用    (阅读:4072)
  6. 研究ext发现ajax跨域实现    (阅读:3686)
  7. 三谈Iframe自适应高度    (阅读:3582)
  8. 使用window.postMessage实现跨域通信    (阅读:3356)
  9. 使用document.domain和iframe实现站内AJAX跨域    (阅读:3243)
  10. 跨域请求的iframe解决方案(2)    (阅读:3104)
QQ技术交流群:445447336,欢迎加入!
扫一扫订阅我的微信号:IT技术博客大学习
© 2009 - 2024 by blogread.cn 微博:@IT技术博客大学习

京ICP备15002552号-1