欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JSONP 跨域共享信息

 更新時(shí)間:2012年08月16日 11:33:52   作者:  
JSONP(JSON with Padding)是資料格式 JSON 的一種“使用模式”,可以讓網(wǎng)頁從別的網(wǎng)域要資料。另一個(gè)解決這個(gè)問題的新方法是跨來源資源共享
由于同源策略,一般來說位于 server1.example.com 的網(wǎng)頁無法與不是 server1.example.com 的服務(wù)器溝通,而 HTML 的 <script> 元素是一個(gè)例外。利用 <script> 元素的這個(gè)開放策略,網(wǎng)頁可以得到從其他來源動(dòng)態(tài)產(chǎn)生的 JSON 資料,而這種使用模式就是所謂的 JSONP。用 JSONP 抓到的資料并不是 JSON,而是任意的 JavaScript,用 JavaScript 直譯器執(zhí)行而不是用 JSON 解析器解析。

下面是我在一個(gè)項(xiàng)目中的應(yīng)用:

描述:域名dev.uc.everychina.com 要獲得域名 dev.members.everychina.com下的數(shù)據(jù)
dev.members.everychina.com的服務(wù)器端代碼:
復(fù)制代碼 代碼如下:

class JsController extends CController {
public function actionIndex() {
$callback = isset($_GET['callback']) ? $_GET['callback'] : '';
$result = array();
$userinfo = Intf_Client_Uc_User::instance()->getLoginUser();
$cid = Everychina_Member::instance()->getCid($userinfo['uid']);
//公司展廳評(píng)分
$room_score = Ec_RoomScore::getInstance();
//獲得展廳老的評(píng)分
$update_status = true;
//重新評(píng)分
if(isset($_GET['action']) && $_GET['action']=='update') {
$score_res = $room_score->getScoreInfo($cid);
$room_score->updateScoreResult($cid,$score_res);
$update_status = true;
}
$result['status'] = $update_status;
$res = $room_score->getScoreResult($cid);
$result['score'] = $room_score->getScoreResultView($res['score']);
if ($callback) {
$js = json_encode($result);
echo "$callback( ($js) );";
}
}

域名 dev.uc.everychina.com 下,前端調(diào)用(html)
復(fù)制代碼 代碼如下:

<a id="update_score" href="#" onclick="ajaxUpdateScore();return false;">update score</a>
<div id="member_score"></div>

javascript
復(fù)制代碼 代碼如下:

   function ajaxUpdateScore(){
if(document.getElementById("member_score_script")) {
var score_script = document.getElementById("member_score_script");
document.body.removeChild(score_script);
}
var score_script = document.createElement("script");
score_script.id = "member_score_script";
score_script.src = 'http://dev.members.everychina.com/index.php?r=js/index&callback=show_score&t='+new Date().getTime();
document.body.appendChild(score_script);
}
function show_score(json) {
if(json.status == true) {
var html = '<p>level:'+json.score.level+'</p>';
html += '<p>msg:'+json.score.msg+'</p>';
html += '<p>score:'+json.score.score+'</p>';
$("#member_score").html(html);
}
}

相關(guān)文章

最新評(píng)論