Access-Control-Allow-Origin'标头出现在请求的资源上。因此不允许原点'null'访问

时间:2015-05-16 10:57:39

标签: javascript jquery ajax browser cors

此问题有很多帖子,但我找不到适合我的问题的解决方案。

这是客户端问题,与服务器无关。我的代码使用IE 11,但不使用Chrome或更糟糕的Firefox。显然这是一个CORS问题。我该怎么做才能使用Chrome?

<html>
<head>
    <script type = "text/javascript" src = "jquery.js"></script>
</head>

<body>
<input type="button" id="btn" value="Get netTV Programs">
<br/><br/>
<div id="channels" />
<BR>
<BR>
<div id="programs" />

<script type="text/javascript">
var objChannels, objPrograms;

var timeDiffBrazil = 4;
var dateMilliSeconds  = new Date().getTime();
dateMilliSeconds=dateMilliSeconds - (timeDiffBrazil*60*60*1000);

var yyyy = new Date().getFullYear();
var monthNo = new Date().getMonth() + 1;
if (monthNo<10)
    monthNo='0' + monthNo;
var dayNo = new Date().getDate();
if (dayNo<10)
    dayNo='0' + dayNo;
var hour = new Date().getHours();
hour = hour - timeDiffBrazil;
if (hour<0)
    {
    hour=0;
    }

var minutes = new Date().getMinutes();
if (minutes<30)
    minutes='00';
else
    minutes='30';

var hour2 = hour + 3;   //2;
var minutes2 = minutes;
if (hour2>24)
    {
    hour2=23;
    minutes2='59';
    }

//alert(yyyy + '-' + monthNo + '-' + dayNo + ' - ' + hour + ':' + minutes + ' - ' + hour2 + ':' + minutes2);

function getTimeFromField(dateTime) {
time=dateTime;
var a = dateTime.indexOf("T");
if (a>-1) {         //found
    time=dateTime.substr(a+1);
    time=time.substr(0,time.length-1);
    }
return time;
}

function getDetails(url) {
        $.ajax({
          crossOrigin: true,
          url: url,
          context: {},
          //dataType: "jsonp",
          success: function(data) {
              //alert(data);
            },
          error: function(e) {
            alert("err= " + e.statusText);
            }
        })
        .done(function( data, textStatus, jqXHR ) {

            var response=jqXHR.responseText;
            var description = "";
            var elenco = "";
            var a = response.indexOf("description");
            if (a>-1) {         //found
                response=response.substr(a+11);
                a = response.indexOf("description");
                if (a>-1) {         //found
                    response=response.substr(a+16);
                    a = response.indexOf("</p>");
                    if (a>-1) {         //found
                        description=response.substr(0,a);
                        response=response.substr(a);
                        a = response.indexOf("Elenco");
                        if (a>-1) {         //found
                            elenco=response.substr(a);      //+12
                            a = elenco.indexOf("</p>");     //br/
                            elenco=elenco.substr(0,a);
                        }
                    }
                }
            }

            alert(description + ' - ' + elenco);

        })  //end .done
};

function callbackChannels(response) {
    objChannels=response;
};

function callbackShows(response) {
    objPrograms=response;
};

$(function() {
    $( '#btn' ).click(function(){

        var url = 'http://programacao.netcombo.com.br/gatekeeper/canal/select?q=id_cidade:23&callback=callbackChannels&json.wrf=callbackChannels&wt=json&rows=300&sort=cn_canal+asc&fl=id_canal+st_canal+cn_canal+nome&fq=nome%3A*'

        $.ajax({
          crossOrigin: true,
          url: url,
          context: {},
          //dataType: "jsonp",
          success: function(data) {
              //alert(data);
            },
          error: function(e) {
            alert("err= " + e.statusText);

            }
        })
        .done(function( data, textStatus, jqXHR ) {

            var url = 'http://programacao.netcombo.com.br/gatekeeper/exibicao/select?q=id_cidade:23&callback=callbackShows&json.wrf=callbackShows&wt=json&rows=100000&sort=id_canal+asc%2Cdh_inicio+asc&fl=dh_fim+dh_inicio+st_titulo+titulo+id_programa+id_canal&fq=dh_inicio%3A%5B';

            url = url + yyyy + '-' + monthNo + '-' + dayNo;
            url = url + 'T' + hour + '%3A' + minutes + '%3A00' + 'Z+TO+';
            url = url + yyyy + '-' + monthNo + '-' + dayNo;
            url = url + 'T' + hour2 + '%3A' + minutes2 + '%3A00' + 'Z';
            url = url + '%5D+dh_fim%3A%5B';

            url = url + yyyy + '-' + monthNo + '-' + dayNo;
            url = url + 'T' + hour + '%3A' + minutes + '%3A00' + 'Z+TO+';
            url = url + yyyy + '-' + monthNo + '-' + dayNo;
            url = url + 'T' + hour2 + '%3A' + minutes2 + '%3A00' + 'Z';

            url = url + '%5D&_=' + dateMilliSeconds;

            $.ajax({
            crossOrigin: true,
            url: url,
            context: {},
            //dataType: "jsonp",
            success: function(data) {
              //alert(data);
                },
            error: function(e) {
                alert("err= " + e.statusText);

                }
            })
            .done(function( data, textStatus, jqXHR ) {
                var html_channels="";
                var html_programs="";
                var html_temp="";
                if (!objChannels)
                    {
                    alert('No data');
                    }
                html_programs="<table><tr><td>" + dayNo + '-' + monthNo + '-' + yyyy;   // + "</td>";
                html_programs = html_programs + " - " + hour + ':' + minutes + '-' + hour2 + ':' + minutes2 + "</td></tr>";
                for (i=0;i<objChannels.response.docs.length;i++)
                    {
                    html_programs=html_programs + "<tr><td nowrap>" + objChannels.response.docs[i].cn_canal + ' ' + objChannels.response.docs[i].nome + "</td>";
                    for (j=0;j<objPrograms.response.docs.length;j++)
                        {
                        if (objPrograms.response.docs[j].id_canal==objChannels.response.docs[i].id_canal)
                            {
                            startTime=getTimeFromField(objPrograms.response.docs[j].dh_inicio);
                            endTime=getTimeFromField(objPrograms.response.docs[j].dh_fim);

                            urlDetails='http://www.netcombo.com.br/tv-por-assinatura/programacao/programa/';
                            urlDetails=urlDetails + objPrograms.response.docs[j].st_titulo + '-' +  + objPrograms.response.docs[j].id_programa;

                            link='<a href="javascript:getDetails(\'' + urlDetails  + '\')"';
                            link=link + '>' + objPrograms.response.docs[j].titulo + '</a>';
                            //console.log(link);
                            html_temp= '<td nowrap>' + startTime + '-' + endTime + '</td><td nowrap>' +  link + '</td>';    // + ' / ' + objPrograms.response.docs[j].st_titulo + ' / ' + objPrograms.response.docs[j].id_programa; // + ' / ' + objPrograms.response.docs[j].id_canal;
                            html_programs=html_programs + html_temp;        //"<BR>"
                            }
                        }
                    html_channels=html_channels + i + ': ' + objChannels.response.docs[i].cn_canal + ' / ' + objChannels.response.docs[i].id_canal + ' / ' + objChannels.response.docs[i].nome + ' / ' + objChannels.response.docs[i].st_canal + "<BR>";
                    html_programs=html_programs + "</tr>";  // + "<BR>";
                    }
                html_programs=html_programs + "</table>" + "<BR>";
                $( '#channels' ).html(html_programs);   // + "<BR><BR>" + html_channels);
            }); //end .done 2
        }); //end .done 1
    }); //end click function
}); //end jQuery function

</script>

</body>
</html>

0 个答案:

没有答案