通过jquery从跨域获取正文内容

时间:2016-04-08 19:10:01

标签: javascript jquery ajax

我想通过ajax interval / refresh从不同的域存储数据(明文)。 例如。这是不同的域:http://live-radio01.mediahubaustralia.com/3SALE/mp3/currentsong

2 个答案:

答案 0 :(得分:0)

也许这会有所帮助。

$.ajax({
    url: "http://live-radio01.mediahubaustralia.com/3SALE/mp3/currentsong",
    type: 'GET',
    success: function(res) {
        $.ajax({
            url: "http://live-radio01.mediahubaustralia.com/3SALE/mp3/currentsong",
            type: 'POST',
            data:res
        });
    }
});

答案 1 :(得分:0)

您可以通过此处解释的“CORS Anywhere”实现此目的:Loading cross domain endpoint with jQuery AJAX

所以,使用其他帖子的代码。你的看起来像这样:

$(document).ready(function() {
  $.ajaxPrefilter(function(options) {
    if (options.crossDomain && jQuery.support.cors) {
      var http = (window.location.protocol === 'http:' ? 'http:' : 'https:');
      options.url = http + '//cors-anywhere.herokuapp.com/' + options.url;
      //options.url = "http://cors.corsproxy.io/url=" + options.url;
    }
  });

  $.get('http://live-radio01.mediahubaustralia.com/3SALE/mp3/currentsong', function(response) {
    console.log(response);
    $("#viewer").html(response);
  });

});

Plnkr.co - 预览&代码