【问题标题】:AngularJS Cross Origin Request using http GET BlockedAngularJS 跨域请求使用 http GET 被阻止
【发布时间】:2017-01-09 20:36:58
【问题描述】:

我需要访问 http://www.wienerlinien.at/ogd_routing/XML_TRIP_REQUEST2?locationServerActive=1&type_origin=any&name_origin=60201095&type_destination=any&name_destination=60200769 (返回一个 XML 文件)

->这会导致跨域请求错误(不幸的是服务只能通过http获得)。

我还在我的服务器中启用了跨域。但是没有任何成功。

gulp.task('browser-sync', function () {
    browserSync({
        notify: true,
        port: 8000,
        server: {
            baseDir: "dist",
            middleware: function (req, res, next) {
                res.setHeader("Access-Control-Allow-Origin", "*");
                next();
            }
        }
    });
});

这是执行http请求的函数:

  this.requestjourney = function (id1, id2) {
        
        var promise = $http({
            method: 'GET',
            timeout: 5000000,
            // url: "http://www.wienerlinien.at/ogd_routing/XML_TRIP_REQUEST2?locationServerActive=1&outputFormat=JSON&type_origin=any&name_origin=Reumannplatz&type_destination=any&name_destination=Stephansplatz", 
            url: "http://www.wienerlinien.at/ogd_routing/XML_TRIP_REQUEST2?locationServerActive=1&type_origin=any&name_origin=" + id1 + "&type_destination=any&name_destination=" + id2,             
            locationServerActive: 1,
            excludedMeans: 0,
            excludedMeans: 1,
            excludedMeans: 3,
            excludedMeans: 4,
            excludedMeans: 5,
            excludedMeans: 6,
            excludedMeans: 7,
            excludedMeans: 8,
            excludedMeans: 9,
            excludedMeans: 10,
            excludedMeans: 11,
            contentType: 'application/x-www-form-urlencoded; charset=utf-8'
        });

        return promise;
    }

是否有任何解决 angularJS 中的交叉输入请求的食谱解决方案?或解决方法?

提前致谢, 沃尔夫冈

【问题讨论】:

  • 当我尝试在服务器端启用 CORS 时,我唯一的经验与 Web Api 2 有关。这是一个有用的链接(假设 Web Api 适用于您的情况)-enable-cors.org/server_aspnet.html

标签: angularjs cors http-get


【解决方案1】:

您确定在服务器端启用了 CORS 吗? 当我从我的网页请求 URL 时,我没有收到“Access-Control-Allow-Origin”标头。 (并且 Firefox 抱怨开发者控制台中缺少标头。)此标头是工作正常的最低要求。

在此处阅读更多信息:https://developer.mozilla.org/en-US/docs/Web/HTTP/Access_control_CORS#Preflighted_requests

如果有疑问,请先尝试使用 jQuery 而不是 Angular,例如使用 $.get(url)。

【讨论】:

  • 我试过jQuery,不幸的是这并没有解决问题。 XMLHttpRequest 无法加载 wienerlinien.at/ogd_routing/……in=any&name_origin=60201095&type_destination=any&name_destination=60200769。请求的资源上不存在“Access-Control-Allow-Origin”标头。因此不允许访问 Origin 'localhost:8000'。
猜你喜欢
  • 2018-09-09
  • 2017-12-14
  • 1970-01-01
  • 2019-10-11
  • 2023-03-03
  • 2014-11-10
  • 2018-12-31
  • 2021-06-26
相关资源
最近更新 更多