为了解决这类问题,就出现了jsonp,专门解决跨域问题。
php
public function getUserInfoJsonpAction()
{
$data = Service_User_InfoModel::getUserInfo($this->_loginUserId);
$this->jsonp(1, $data);
}
/**
* 封装:jsonp跨域
*/
public function jsonp($errno = 0, $data = '', $message = '')
{
$errno = intval($errno);
$result = array(
'errno' => $errno,
'message' => $message,
'data' => $data
);
echo $_GET['callback'] . "(" . json_encode($result) . ")";
exit;
}
js(基于seajs框架)
define('public.js', ['widget/search.js', 'module/home/notice.js'], function (require, exports, module) {
_url = {
userInfo: 'http://ucenter.xxx.com/api/getUserInfoJsonp/',
};
//获取用户信息
requestUrl(_url.userInfo, {}, function (userInfo) {
globalData.user_info = userInfo;
globalData.user_info.uid = userInfo.id;
console.log(userInfo);
}, true);
});