An example of ajax cross-origin retrieval of website json data, ajaxjson

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.