例如:客戶端的域名是client.runoob.com,而請求的域名是server.runoob.com。
如果直接使用ajax訪問,會有以下錯誤:
XMLHttpRequest cannot load http://server.runoob.com/server.php. No 'Access-Control-Allow-Origin' header is present on the requested resource.Origin 'http://client.runoob.com' is therefore not allowed access.
header('content-type:application/json;charset=utf8');
// 指定允許其他域名訪問
header('Access-Control-Allow-Origin:*');
// 響應類型
header('Access-Control-Allow-Methods:POST');
// 響應頭設置
header('Access-Control-Allow-Headers:x-requested-with,content-type');
1、允許單個域名訪問
指定某域名(http://client.runoob.com)跨域訪問,則只需在http://server.runoob.com/server.php文件頭部添加如下代碼:
header('Access-Control-Allow-Origin:http://client.runoob.com');
2、允許多個域名訪問
指定多個域名(http://client1.runoob.com、http://client2.runoob.com等)跨域訪問,則只需在http://server.runoob.com/server.php文件頭部添加如下代碼:
$origin = isset($_SERVER['HTTP_ORIGIN'])? $_SERVER['HTTP_ORIGIN'] : ''; ($_SERVER['HTTP_ORIGN']----獲得請求訪問的域名)
$allow_origin = array(
'http://client1.runoob.com',
'http://client2.runoob.com'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
}
3、允許所有域名訪問
允許所有域名訪問則只需在http://server.runoob.com/server.php文件頭部添加如下代碼:
header('Access-Control-Allow-Origin:*');
二。。
今天我寫的是PHP AJAX JSONP使用的實例。
test.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="jquery-1.5.2.min.js"></script>
<script src="ajax.js"></script>
</head>
<body>
</body>
</html>
ajax.js
$.ajax({
type : "post",
url : "ajax.php",
dataType : "jsonp",
data : {'name':'zjx'}
jsonp: "callback",//傳遞給請求處理程序或頁面的,用以獲得jsonp回調函數名的參數名(默認為:callback)
jsonpCallback:"success_jsonpCallback",//自定義的jsonp回調函數名稱,默認為jQuery自動生成的隨機函數名
success : function(json){
alert('success');
},
error:function(){
alert('fail');
}
});
ajax.php
<?php
$data = $_POST['name'];
$callback = $_GET['callback']; $_REQUEST['callback']
echo $callback.'('.json_encode($data).')';
exit;
?>
實例2
test.html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>test</title>
<script src="jquery-1.5.2.min.js"></script>
<script src="ajax.js"></script>
</head>
<body>
<form name="form">
<input type="text" name="sex">
<input type="text" name="age">
<input type="button" id="btn" value="button" />
</form>
</body>
</html>
ajax.js
$(document).ready(function(){
$("#btn").click(function(k) {
//...
var j = $("form").serializeArray();//序列化name/value
$.ajax({
type: 'GET', //這里用GET
url: 'ajax.php?name=zjx',
dataType: 'jsonp', //類型
data : '',
jsonp: 'callback', //jsonp回調參數,必需
async: false,
success: function(result) {//返回的json數據
alert(result.message); //回調輸出
result = result || {};
if (result.msg=='err'){
alert(result.info);
}else if (result.msg=="ok"){
alert('提交成功');
}else{
alert('提交失敗');
}
},
timeout: 3000
})
//...
});
});
ajax.php
<?php
$callback = isset($_GET['callback']) ? trim($_GET['callback']) : ''; //jsonp回調參數,必需
$date = array("name"=>$_GET['name'], "msg"=>$_GET['message']);
$date["msg"]="err";
$date["info"]="因人品問題,發送失敗";
$tmp= json_encode($date); //json 數據
echo $callback . '(' . $tmp .')'; //返回格式,必需
'?>