Jquery 跨域取数据--$.ajax 和$.getJSON
一般跨域用到的两个方法为:$.ajax 和$.getJSON
比如PHP处理如下:
<?php defined('BASEPATH') OR exit('No direct script access allowed'); define('APPID', 'wx04369bbf9676b462'); define('APPSECRET', 'ba58586bd757486765d774f40326c6f5'); /* * JS安全接口授权 * @author czt20160413 */ class Jsapisign extends CI_Controller{ public function __construct(){ parent::__construct(); } public function index(){ header("Access-Control-Allow-Origin:*");//允许跨域才可以进行 $time_stamp = time(); $jsapi_ticket = $this->wxinterface->getJsApiTicket(APPID, APPSECRET); $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? 'https://' : 'http://'; $url = $protocol.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; $nonce_str = createNonceStr(); $signature_str = getSignature($jsapi_ticket, $url, $time_stamp, $nonce_str); $data['appid'] = APPID; $data['timestamp'] = $time_stamp; $data['noncestr'] = $nonce_str; $data['signature'] = $signature_str; $data['url'] = $url; ajax($data); } }
获取链接为:http://wx.5usport.com/Jsapisign

JS写法为:
<script> $(function(){ $.getJSON("http://wx.5usport.com/Jsapisign", function (data) { alert(data.appid); } ); }) </script>
这样就能获取到json里面的数据了,真好,以后可以经常这样用啦,
浙公网安备 33010602011771号