An example of ajax cross-origin retrieval of website json data, ajaxjson
Because your company's project needs to call the video address
1: when it is a link: address directly searched in the player Database
2: when the external link is used: directly use window. location. href ('database lookup address ')
3: when it is an H5 link: Use <ifram src = "database lookup address"> play
4: when linking other websites, go to a third-party website to read json information and put the json data as a url in the player.
When the value is 4, the format is incorrect when I use json.
When jsonp is used to solve cross-origin problems, the returned format cannot be received.
So I use
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; }
Pass in a url. This method will return the content of the website after reading it,
So I passed the url to this method using ajax at the front end, and the return type is json.
Use data. result. data name to obtain json data in the url.
The above example of ajax cross-origin access to the website's json data is all the content shared by Alibaba Cloud xiaobian. I hope you can give us a reference and support for our customer base.