由于自己的公司的項(xiàng)目需要調(diào)用視頻地址
1:當(dāng)為鏈接時(shí):直接在播放器用數(shù)據(jù)庫查找的地址
2:當(dāng)為外部鏈接時(shí):直接用window.location.href('數(shù)據(jù)庫查找的地址')
3:當(dāng)為H5鏈接時(shí):使用<ifram src="數(shù)據(jù)庫查找的地址">播放
4:當(dāng)為其余網(wǎng)站鏈接時(shí),要去第三方網(wǎng)站讀取json信息然后把json數(shù)據(jù)作為url放在播放器中
當(dāng)為4時(shí),我使用json時(shí)會(huì)出格式錯(cuò)誤
當(dāng)用jsonp解決跨域問題時(shí),會(huì)出現(xiàn)返回格式接收不到
所以我用
public static String analysisUrl(String url){ HttpURLConnection httpConnection = null; String output = ""; try { URL targetUrl = new URL(url); httpConnection = (HttpURLConnection) targetUrl.openConnection(); httpConnection.setDoOutput(true); httpConnection.setRequestMethod("GET"); httpConnection.setRequestProperty("Content-Type", "application/json"); InputStreamReader isr = new InputStreamReader(httpConnection .getInputStream(),"utf-8"); BufferedReader responseBuffer = new BufferedReader(isr); output = responseBuffer.readLine(); } catch (Exception e) { } finally { httpConnection.disconnect(); } return output; }
傳遞一個(gè)url進(jìn)去,這個(gè)方法會(huì)將網(wǎng)站的內(nèi)容讀取之后return出來,
所以我在前臺(tái)用ajax傳遞url到這個(gè)方法,返回類型為json
用data.result.數(shù)據(jù)名 得到url里面的json數(shù)據(jù)。
以上這篇ajax跨域獲取網(wǎng)站json數(shù)據(jù)的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持武林網(wǎng)。
新聞熱點(diǎn)
疑難解答
圖片精選