zoukankan      html  css  js  c++  java
  • ajax跨域获取网站json数据

    由于自己的公司的项目需要调用视频地址

    1:当为链接时:直接在播放器用数据库查找的地址

    2:当为外部链接时:直接用window.location.href('数据库查找的地址')

    3:当为H5链接时:使用<ifram src="数据库查找的地址">播放

    4:当为其余网站链接时,要去第三方网站读取json信息然后把json数据作为url放在播放器中

    当为4时,我使用json时会出格式错误

    当用jsonp解决跨域问题时,会出现返回格式接收不到

    所以我用

    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数据。
  • 相关阅读:
    solr7.7.0 添加core (二)
    centos 安装solr7.7+tomcat8.5.31+jdk1.8 环境搭建(一)
    springboot 切面编程 日志模块
    Mysql 优化
    mysql查询某个字段中是否有重复的值
    php for循环字母
    layui.table.toolbar里的内容加判断
    phpmyadmin 导入大文件配置
    解决laravel5.2 使用ajax时的 VerifyCsrfToken问题
    火狐浏览器禁止缓存
  • 原文地址:https://www.cnblogs.com/dudadi/p/8029758.html
Copyright © 2011-2022 走看看