2014-09-10 22 views
1

我在本地服務器上運行Google Super Proxy項目並嘗試從Google appspot網站中提取託管的JSON查詢數據。CORS支持不能與域名一起使用

http://userTest.appspot.com/query?id=ahJzfmNvbm5vcnBoSDfsFEWDSDxsaXBzMjRyFQsSCEFwaVF1ZXJ5GICAgICAgIAKDA 

我一直遇到的問題是,我不允許從我的本地服務器訪問此網頁,因爲我沒有CORS支持。我試過JavaScript和JQuery都是爲了啓用CORS支持,但我仍然在控制檯中收到兩條錯誤消息。任何人都知道什麼可以幫助

1)

Failed to load resource: the server responded with a status of 405 (Method Not Allowed) http://userTest.appspot.com/query?id=ahJzfmNvbm5vcnBoaWEEWWEWxsaXBzMjRyFQsSCEFwaVF1ZXJ5GICAgICAgIAKDA 

2)

XMLHttpRequest cannot load http://userTest.appspot.com/query?id=ahJzfmNvbm5vcnBoaWxsaXBzMjREWEADSdyFQsSCEFwaVF1ZXJ5GICAgICAgIAKDA. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8888' is therefore not allowed access. 

的index.html:

<!doctype html> 
<html> 

    <head> 
     <title>Google Super Proxy Test</title> 
     <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
     <script src="chart-options.js"></script> 
     <script src="Chart.min.js"></script> 
    </head> 

<body> 

    <div style="width: 50%"> 
     <canvas id="sessions-graph" height="450" width="600"></canvas> 
    </div> 


</body> 

</html> 

圖表-options.js:

$.ajax({ 
    type: 'GET', 
    url: 'http://userTest.appspot.com/query?id=ahJzfmNvbm5vcnBoaWxsaXBzMSDFSDFSjRyFQsSCEFwaVF1ZXJ5GICAgICAgIAKDA', 
    contentType: 'json', 
    crossDomain: true, 
    headers: { 'Access-Control-Allow-Origin': '*'}, 
    success: function(data) { 
     $.each(data, function(index, element) { 
      alert(element.name); 
    }) 
}, 
    xhrFields: { 
     withCredentials: true 
     }, 

    error: function (json) { 
     debugger; 
    } 
}); 



    var barChartdata = { 
     labels: [], 
     datasets: [ 
      { 
       label: "My First dataset", 
       fillColor: "rgba(220,220,220,0.5)", 
       strokeColor: "rgba(220,220,220,0.8)", 
       highlightFill: "rgba(220,220,220,0.75)", 
       highlightStroke: "rgba(220,220,220,1)", 
       data: [] 
      } 
     ] 
    }; 


    var options = { 

     //Boolean - Whether the scale should start at zero, or an order of magnitude down from the lowest value 
     scaleBeginAtZero : true, 

     //Boolean - Whether grid lines are shown across the chart 
     scaleShowGridLines : true, 

     //String - Colour of the grid lines 
     scaleGridLineColor : "rgba(0,0,0,.05)", 

     //Number - Width of the grid lines 
     scaleGridLineWidth : 1, 

     //Boolean - If there is a stroke on each bar 
     barShowStroke : true, 

     //Number - Pixel width of the bar stroke 
     barStrokeWidth : 2, 

     //Number - Spacing between each of the X value sets 
     barValueSpacing : 5, 

     //Number - Spacing between data sets within X values 
     barDatasetSpacing : 1, 

     //Boolean - Set if responsive or not 
     responsive : true 

    } 

window.onload = function(){ 

    // Get the context of the canvas element 
    var ctx = document.getElementById("sessions-graph").getContext("2d"); 
    var sessionsGraph = new Chart(ctx).Bar(barChartdata, options); //Create a chart with "data" array 

}; 

回答

1

Access-Control-Allow-Origin響應標頭。

您要求提供數據的服務器必須在HTTP響應中提供它。編輯負責生成http://userTest.appspot.com/query的代碼以包含它。

它不屬於請求標頭。您的腳本無法授予自己訪問任何網站的權限。

通過使其成爲請求標題,您正在觸發預檢選項請求(這可能是因爲您的方法不允許錯誤)的可能原因。

相關問題