donglang7236 2014-01-06 19:27
浏览 46
已采纳

无法通过ajax调用从“数据库”获取数据“No'Access-Control-Allow-Origin'”

I'm trying to do an ajax GET call to get some information out of my database. Offline (trough localhost) everything works perfectly. Online I get the same error over and over again:

"No 'Access-Control-Allow-Origin' header is present on the requested resource." As you can see on following link: http://d.pr/i/pHR4

The javascript code:

    function loadnewsong(){

    var url = "http://www.sonder.be/?nextsong=true&id=" + $('#musicid').text();

    $.ajax({
        type:"GET",
        url: url,
        dataType:"json",
        success: function(data){
            console.log(data);

            $('#title').fadeOut(function(){

            $('#title').text(data[0].title);
                $('#title').fadeIn();
            });

            $('#musicid').text(data[0].id);

            audio.pause();
            $('#srcmp3').attr('src',"assets/media/" + data[0].mp3);
            $('#srcogg').attr('src',"assets/media/" + data[0].ogg);
            audio.load();
            audio.play();

            url = "http://www.sonder.be/?nextsong=true&id=" + $('#musicid').text();

        }
    });
}

What can I possibly be doing wrong?

  • 写回答

1条回答 默认 最新

  • duanpie4763 2014-01-06 19:29
    关注

    sonder.be is asking the browser to get data from www.sonder.be and www.sonder.be isn't giving it permission to share that data with sonder.be.

    Since these are, presumably, different hostnames that point to the same site, you have wto simple approaches to fix this:

    1. Pick one of the two host names to be canonical and issue 301 redirects for all resources on the other to it.
    2. Use relative URIs.

    Further reading:

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3加ant-design-vue无法渲染出页面
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序