Home>Article>Web Front-end> How to use ajax to obtain json data across domains

How to use ajax to obtain json data across domains

php中世界最好的语言
php中世界最好的语言 Original
2018-03-30 16:38:39 1449browse

This time I will show you how to operate ajax cross-domain acquisition of json data. What are theprecautionsfor ajax cross-domain acquisition of json data. The following is a practical case, let's take a look.

Because my company’s project needs to callVideoAddress

1: When it is a link: directly search in the database in the player The address

2: When it is an external link: directly use window.location.href('the address searched in the database')

3 : When linking to H5: use Play

4: When linking to other websites, go to the third-party website to read json information and then put the json data as url in the player

When it is 4, a format error will occur when I use json

When using jsonp to solve cross-domain problems, It will appear that the return 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; }

to pass a url in. This method will read the content of the website and return it,

So I use ajax to pass the url to this method at the front desk, and the return type is json

Use data.result.data name to get the json data in the url.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Ajax+PHP code to change status and delete without refreshing

How to implement Ajax client Asynchronously calling the server

The above is the detailed content of How to use ajax to obtain json data across domains. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn