1.ajax的jsonp格式
$.ajax({
type : "GET",
url : "http://127.0.0.1/raid/jquery_learning/ajax_learning/php/index.php?number="+$("#keyword").val(),
dataType : "jsonp",
jsonp : "callback",
success : function(data) {
if (data.success) {
$("#searchResult").html(data.msg);
} else {
$("#searchResult").html("出现错误"+data.msg);
}
},
error : function(jqXHR) {
alert("发生错误"+jqXHR.status);
}
})
2.html的
header("Access-Control-Allow-Method:POST,GET");
header("Access-Control-Allow-Origin: *");
3.多个地址跨域访问
//跨域访问的时候才会存在此字段
$origin = isset($_SERVER['HTTP_ORIGIN']) ? $_SERVER['HTTP_ORIGIN'] : '';
$allow_origin = array(
'http://www.a.com',
'http://www.b.com'
);
if(in_array($origin, $allow_origin)){
header('Access-Control-Allow-Origin:'.$origin);
header('Access-Control-Allow-Methods:POST');
header('Access-Control-Allow-Headers:x-requested-with,content-type');
}
网友评论