Ajax getjson 跨域通信 php+jquery

网站A的表单提交部分:

<script type=”text/javascript” src=”jquery-1.4.4.min.js” ></script>
<script type=”text/javascript”>

$(“#myform”).submit(function(){
var name = $(“#name”).val();
var phone = $(“#phone”).val();
$.getJSON(“http://www.siteb.com/do.php?name=” + name + “&phone=” + phone + “&format=json&jsoncallback=?“,
function(data){
if(data.status == ‘ok’){
alert(‘操作成功!’);
}else{
alert(‘操作失败!’);
}
});
return false;
});
</script>

网站B的输出json部分:

<?php

$name  = isset($_GET[‘name’]) ? trim($_GET[‘name’]) : ”;
$phone = isset($_GET[‘phone’]) ? trim($_GET[‘phone’]) : ”;

$return = array();

if(插入数据库成功){

$return[‘status’] = ‘ok’;
$return[‘message’] = ”;

}else{

$return[‘status’] = ‘error’;
$return[‘message’] = ‘操作失败’;

}

die($_GET[‘jsoncallback’] . ‘(‘ . json_encode($return) . ‘)‘);

?>