javascript - CORS 支持不适用于域

标签 javascript jquery ajax cors

我正在本地服务器上运行 Google super 代理项目,并尝试提取从 Google appspot 网站托管的 JSON 查询数据。

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>

图表选项.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

};

最佳答案

Access-Control-Allow-Origin 是一个响应 header 。

您要求提供数据的服务器必须在 HTTP 响应中提供该数据。编辑负责生成 http://userTest.appspot.com/query 的代码以包含它。

它不属于请求 header 。您的脚本无法授予自身访问任何网站的权限。

通过将其设为请求 header ,您将触发预检 OPTIONS 请求(这可能是导致方法不允许错误的原因)。

关于javascript - CORS 支持不适用于域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25769857/

相关文章:

javascript - 在每个 Ember Handlebars 中迭代数组

javascript - 正则表达式获取完整的可选部分(如果存在)

javascript - 如何在没有表单的情况下通过 javascript 使用 post 发送数据?

jQuery 无冲突

javascript - 如何在不取消页面卸载的情况下发送 AJAX PUT 请求?

javascript - Firebase 如何链接用电话号码创建的帐户

javascript - Google 表格上的提交按钮

ShowDay 之前的 jQuery 日期选择器

javascript - 设置间隔为ajax形式

javascript - 在ajax成功后动态更新/刷新html而无需重新加载页面