方案一:callback
方案二:crossDomain:true
一:
js:
$(document).ready(function(){
var url='http://localhost:8080/ajax.php?"+"?id=1&callback=?';
$.ajax({
url:url,
dataType:'jsonp',
processData: false,
type:'get',
success:function(data){
alert(data.name);
},
error:function(XMLHttpRequest, textStatus, errorThrown) {
alert(XMLHttpRequest.status);
alert(XMLHttpRequest.readyState);
alert(textStatus);
}
});
php:
$out=array();
$callback=$_GET['callback'];
toJson($out,$callback);
function toJson($out,$callback){
if($callback){
die($callback.'('.json_encode($out).')');
}else{
die(json_encode($out));
}
}
二:
js:
$.ajax({
url: 'http://m.house365.com/H5/game/index.php?g=Wap&m=Fangjiaohui&a=apply',
type: 'POST',
crossDomain: true,
data: datas,
dataType: 'json',
success: function(response){
if (response.code == 1) {
malert.find('.inner').text('报名成功');
setTimeout(function(){
closeDialog($('.applay-dialog'));
},2000);
} else if (response.code == 0) {
malert.find('.inner').text('已经报过名啦');
} else {
malert.find('.inner').text('报名失败');
}
malert.show();
setTimeout(function(){
malert.hide();
},1000);
},
error: function(){
malert.find('.inner').text('报名失败');
malert.show();
setTimeout(function(){
malert.hide();
},1000);
}
});
php:
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: POST');
header('Access-Control-Max-Age: 1000');