加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

巧妙解决AJAX跨域问题

发布时间:2020-12-16 00:44:29 所属栏目:百科 来源:网络整理
导读:?php/***ajaxproxy*ajax跨域解决办法*@authorsuconghousuconghou@126.com*@versionv1.1*@bloghttp://blog.suconghou.cn*@update2014.2.26*@修正了get时忽略了端口号的问题*/classajax{private$url;///真实的ajax地址private$get;private$post;private$get_st
<?php

/**
*ajaxproxy
*ajax跨域解决办法
*@authorsuconghou<suconghou@126.com>
*@versionv1.1
*@bloghttp://blog.suconghou.cn
*@update2014.2.26
*@修正了get时忽略了端口号的问题
*/
classajax
{

private$url;///真实的ajax地址
private$get;
private$post;
private$get_string;
private$post_string;
private$result;

function__construct()
{
isset($_REQUEST['url'])||exit('noavailableurl');
$this->url=$_REQUEST['url'];
foreach($_GETas$key=>$value)
{
if($key=='url')continue;
$this->get[$key]=$value;
}
foreach($_POSTas$key=>$value)
{
if($key=='url')continue;
$this->post[$key]=$value;
}
if(!empty($this->get))
{
$this->get_string=$this->implode_with_key($this->get);
}
if(!empty($this->post))
{
$this->post_string=$this->implode_with_key($this->post);
}
//$this->debug();
$this->ajax();

}

functiondebug($debug=1)
{

var_dump($this->get);
var_dump($this->post);
var_dump($this->get_string);


}
functionajax()
{

if(empty($this->post))///没有post数据,但可能有get
{
$this->get();
}
else//可能有post,有get
{
$this->post();
}
echo$this->result;

}

///三种版本的post,get,优先使用curl
functionpost()
{
if(extension_loaded('curl'))
{
$url=$this->query_string();
$ch=curl_init();
curl_setopt_array($ch,array(CURLOPT_URL=>$url,CURLOPT_RETURNTRANSFER=>1,CURLOPT_POST=>1,CURLOPT_POSTFIELDS=>$this->post_string));
$this->result=curl_exec($ch);
curl_close($ch);


}
elseif(function_exists('fsockopen'))
{
$parts=parse_url($this->url);
$fp=fsockopen($parts['host'],isset($parts['port'])?$parts['port']:80,$errno,$errstr,10);
if(!$fp)die("$errstr($errno)");
$url=$this->query_string(1);
$out='POST'.$url."rnContent-type:application/x-www-form-urlencodedrn"."Content-length:".strlen($this->post_string)."rnConnection:closernrn".$this->post_string;
//exit($out);
fwrite($fp,$out);

while($str=trim(fgets($fp,4096)))
{
$header.=$str;
}
while(!feof($fp))
{
$data.=fgets($fp,4096);
}

$this->result=$data;


}
else
{
$context=array(
'http'=>array(
'method'=>'POST','header'=>'Content-type:application/x-www-form-urlencodedrn'.
'Content-length:'.strlen($this->post_string)+8,'content'=>$this->post_string)
);
$stream_context=stream_context_create($context);
$data=file_get_contents($this->query_string(),false,$stream_context);
$this->result=$data;

}

}
functionget()
{

if(extension_loaded('curl'))//已修正端口号问题
{
$ch=curl_init();
$url=$this->query_string();

curl_setopt_array($ch,CURLOPT_RETURNTRANSFER=>1));
$this->result=curl_exec($ch);
curl_close($ch);

}
elseif(function_exists('fsockopen'))
{
$parts=parse_url($this->url);
$fp=fsockopen($parts['host'],10);
if(!$fp)die("$errstr($errno)");
$url=$this->query_string(1);
$out='GET'.$url."rnConnection:Closernrn";
fwrite($fp,$out);
while($str=trim(fgets($fp,4096);
}
$this->result=$data;

}
else
{

$url=$this->query_string();
$this->result=file_get_contents($url);


}


}

functionimplode_with_key($assoc,$inglue='=',$outglue='&')
{
$return=null;
foreach($assocas$tk=>$tv)$return.=$outglue.$tk.$inglue.$tv;
returnsubstr($return,1);
}

functionquery_string($type=0)
{
$parts=parse_url($this->url);
$host_port=$parts['host'];
if($parts['port'])
{
$host_port.=':'.$parts['port'];
}

if(empty($parts['query']))
{
$parts['query']=$this->get_string;
}
else
{
if(!empty($this->get_string))
{
$parts['query'].='&'.$this->get_string;
}

}
if($type)
{
$url=$parts['path'].'?'.$parts['query']."HTTP/1.1rnHost:".$host_port;
}
else
{
$url=$parts['scheme'].'://'.$host_port.$parts['path'].'?'.$parts['query'];
}
//exit($url);
return$url;

}


}


$ajax=newajax();


在发送AJAX时提交参数url给ajax.php文件,url为你想要发送异步请求的ajax页面.例如

varurl="./ajax.php?url=http://my.oschain.net?a=1&b=2";
	$.get(url,{name:'11',pass:'11260',htm:'html'},function(data){
		$('#text').html(data);
	});
///此时会发送get数据name,pass,htm到http://my.oschain.net?a=1&b=2,故发送get时,get数据可以写在url后面,也可以单独,并且url参数也可以单独.
///或者

varurl="./ajax.php?url=http://127.0.0.2:8088/3.php?a=1&b=3";
	$.post(url,function(data){
		$('#text').html(data);
	});
///这样会发送get中的a,bpost中的name,htm
///并且无论get还是post,你都可以将url单独传送,不区分get和post,///如
varurl="./ajax.php";
	$.post(url,{url:'http://my.oschina.net/',name:'11',function(data){
		$('#text').html(data);
	});

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读