前端之家收集整理的这篇文章主要介绍了
ajax跨域调用示例,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>jsonp测试例子</title>
<script type="text/javascript" src="http://www.yzswyl.cn/js/jquery.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function(){
$.ajax({
type: "get",
async: false,
//url: "http://local.com/jsonp.PHP",
url: "http://www.ogtest.com/login/login/checkpwd/",
data:{'crossdomain':1},
dataType: "jsonp",
jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback)
jsonpCallback:"FeedBackState",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名
success: function(data){
var result;
$.each(data,function(i,v){
result=v;
});
if(result.result == '1'){
alert('ok');
}
},
error: function(){
alert('fail');
}
});
});
</script>
</head>
<body>
远程数据如下:<br/>
<div id="remote"></div>
</body>
</html>
http://www.ogtest.com/login/login/checkpwd PHP代码如下:
@H_
301_47@$loginId = stringHelper::searchTrim($this->getParam("loginId"));
$pwd = stringHelper::searchTrim($this->getParam("pwd"));
$userIdRemember = stringHelper::searchTrim($this->getParam("userIdRemember"))?TRUE:FALSE;
$arrCondition = array();
$arrCondition["loginId"] = $loginId;
$arrCondition["pwd"] = $pwd;
//记住
用户登录状态
$userRemember = $this->_request->getCookie('userRemember');
$arrCondition['userRemember'] = json_decode($userRemember,true);
if ($this->getParam("chkUserIdRemember") == "on") {
$arrCondition["userIdRemember"] = TRUE;
} else {
$arrCondition["userIdRemember"] = FALSE;
}
$crossdomain = stringHelper::searchTrim($this->getParam("crossdomain"));
$return = $this->_facade->pbLogin($arrCondition);
if($crossdomain==false){
//非跨域
调用
if($return['isError']==false){
//密码和账号正确
$arrOutPut = array("result"=>'1');
}else{
//密码和账号不正确
$arrOutPut = array("result"=>'0');
}
jsonHelper::outPutJson($arrOutPut);
}else{
//跨域
调用
if($return['isError']==false){
//密码和账号正确
$arrOutPut = array("result"=>'1');
$str = '[{"result":"1"}]';
}else{
//密码和账号不正确
$str = '[{"result":"0"}]';
}
$jsonp = $_REQUEST["callback"];
$str = $jsonp . "(" .$str.")";
echo $str;
exit;