首页 > 解决方案 > ASPX API 在 Chrome 浏览器中被阻止

问题描述

$.ajax({
            type: "GET",
            url: "http://<api URL>/xml_full_new_york.aspx",
            contentType: "application/xml; charset=utf-8",
            dataType: "xml",
            async: true,
            error: function (jqXHR, textStatus, errorThrown) {
            },
            success: function (data) {
                $(data).find('stockquotes').each(function () {
                    var sTitle = $(this).find('CurrentPrice').text();
                    $('#stockTickerNYSE').text("$" + sTitle);
                });
            }
        });

在 Google Chrome 中,上述 API 被阻止。但它在 IE 浏览器中运行良好。在 Chrome 控制台中,我发现错误:

“通过访问控制检查:请求的资源上不存在 'Access-Control-Allow-Origin' 标头。因此不允许访问 Origin 'null'。响应的 HTTP 状态代码为 403。 "

标签: jqueryajaxxmlapijquery-ajaxq

解决方案


这是CORS问题,您需要添加header("Access-Control-Allow-Origin: *");后端代码。


推荐阅读