ajax跨域獲取網站json數據

因爲本身的公司的項目須要調用視頻地址ajax

1:當爲連接時:直接在播放器用數據庫查找的地址數據庫

2:當爲外部連接時:直接用window.location.href('數據庫查找的地址')json

3:當爲H5連接時:使用<ifram src="數據庫查找的地址">播放跨域

4:當爲其他網站連接時,要去第三方網站讀取json信息而後把json數據做爲url放在播放器中app

當爲4時,我使用json時會出格式錯誤jsonp

當用jsonp解決跨域問題時,會出現返回格式接收不到網站

因此我用url

 

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;
    }

 

傳遞一個url進去,這個方法會將網站的內容讀取以後return出來,
因此我在前臺用ajax傳遞url到這個方法,返回類型爲json
用data.result.數據名 獲得url裏面的json數據。
相關文章
相關標籤/搜索