在 Http get 请求 angularjs 上调用 Https url

calling Https url on Http get request angularjs

我有一个 Https url 并想发送请求以从 URL 获取数据,场景 1: 来自我的浏览器 如果我点击 Url 我会得到响应,而从我的 Angularjs 应用程序我总是得到一个错误401 ,但是如果我从浏览器中点击 Api 我总是会得到正确的响应

出于安全原因,我不能在这里使用 Url,但我想要的是:

  $http({
        method: "GET",
        url: "https://urlAdresss/",
        headers: {
            "Accept-Language": "en-US, en;q=0.8",
            "Content-Type": "application/json;charset=UTF-8"
        },
    }).then(function (_response) {
        console.log(_response
     }

我总是未经授权我是网络以及在控制台上任何帮助将不胜感激,

但是如果我从浏览器中点击相同的 Url 我会得到响应 这意味着后端工作正常

我想我在 get 请求中遗漏了一些东西,这就是为什么会出现错误

这似乎是一个 CORS(Cross-Origin 资源共享)问题。

在 AngularJS 端,您应该使用以下配置,以便 $http 服务自动向 http 请求发送授权 headers。

var app = angular.module('app', [])
    .config(function($httpProvider) {
        $httpProvider.defaults.useXDomain = true;
        $httpProvider.defaults.withCredentials = true;
    });

并且在后端,您应该明确指定允许的来源(例如 http://localhost:8888)。
另外,请注意 here.

中的一些要点

If you want to allow credentials then your Access-Control-Allow-Origin must not use *.