Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery AngularJS调用在邮递员中工作,但在AngularJS应用程序中不工作-对飞行前请求的响应不';无法通过访问控制检查_Jquery_Angularjs_Cors - Fatal编程技术网

Jquery AngularJS调用在邮递员中工作,但在AngularJS应用程序中不工作-对飞行前请求的响应不';无法通过访问控制检查

Jquery AngularJS调用在邮递员中工作,但在AngularJS应用程序中不工作-对飞行前请求的响应不';无法通过访问控制检查,jquery,angularjs,cors,Jquery,Angularjs,Cors,我需要angularjs的Cors, 我没有使用cors for webapi,因为我使用的是其他人的api, 我只是想用angularjs通过基本身份验证获取数据, 我曾经尝试过使用POSTMAN,它工作得很好,但是使用我的angularjs应用程序它不工作 我尝试了很多,但它显示错误如下: var myApp = angular.module("myApp", ['ngCookies']); myApp.config(['$httpProvider', function ($httpProv

我需要angularjs的Cors, 我没有使用cors for webapi,因为我使用的是其他人的api, 我只是想用angularjs通过基本身份验证获取数据, 我曾经尝试过使用POSTMAN,它工作得很好,但是使用我的angularjs应用程序它不工作 我尝试了很多,但它显示错误如下:

var myApp = angular.module("myApp", ['ngCookies']);
myApp.config(['$httpProvider', function ($httpProvider) {

    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}

]);
myApp.controller("GetDataController", ["$scope", "$http", function ($scope, $http) {
    $scope.GetToken = function () {
        debugger;
        var appdata = $.param({
            grant_type: 'credentials',
            scope: 'Customer',
        });

        $http({
            async: true,
            crossDomain: true,
            method: 'GET',
             url: 'https://api.url.net/token',
             data: appdata,
            headers: {

                "username": "847fd9f9fg9h7h66jl8ljdggff",
                "password": "302kfsg7fhhjh0dgjngfdjd",
                "Authorization": "Basic Mufdnfaffa8439flg8g"

            }
        }).then(function (response) {
            debugger;
            $scope.displaymessage = response.data;

        }, function errorCallback(response) {
            $scope.displaymessage = response;
            console.log(response)
        });
    };

}])
未能加载资源:服务器以401状态响应 (未经授权)index.html:1 XMLHttpRequest无法加载 . 对飞行前请求的响应未通过 访问控制检查:未显示“访问控制允许原点”标题 在请求的资源上显示。来源“”是 因此不允许访问。响应的HTTP状态代码为401

我尝试了以下示例代码:

var myApp = angular.module("myApp", ['ngCookies']);
myApp.config(['$httpProvider', function ($httpProvider) {

    $httpProvider.defaults.useXDomain = true;
    $httpProvider.defaults.withCredentials = true;
    delete $httpProvider.defaults.headers.common['X-Requested-With'];
}

]);
myApp.controller("GetDataController", ["$scope", "$http", function ($scope, $http) {
    $scope.GetToken = function () {
        debugger;
        var appdata = $.param({
            grant_type: 'credentials',
            scope: 'Customer',
        });

        $http({
            async: true,
            crossDomain: true,
            method: 'GET',
             url: 'https://api.url.net/token',
             data: appdata,
            headers: {

                "username": "847fd9f9fg9h7h66jl8ljdggff",
                "password": "302kfsg7fhhjh0dgjngfdjd",
                "Authorization": "Basic Mufdnfaffa8439flg8g"

            }
        }).then(function (response) {
            debugger;
            $scope.displaymessage = response.data;

        }, function errorCallback(response) {
            $scope.displaymessage = response;
            console.log(response)
        });
    };

}])

当它在POSTMAN中工作时,我应该在哪里进行更改,为什么它在我的应用程序中不工作。

CORS出现在跨域请求的情况下。也就是说,当一个请求从一个域发送到另一个域时

来自
Postman
的呼叫不是从
发送的(因为
Postman
就像一个“独立”的应用程序,而不是一个托管了某个域的网站)。所以在这种情况下,CORS并没有出现。这是更好的解释

但是,您的
Angular JS
应用程序必须托管在某个域上。例如,
localhost
myexample.com
。因此,当从应用程序发送相同的呼叫时,需要CORS

CORS必须在服务器上实现,而不是在应用程序中实现。必须将服务器配置为接受来自特定域或所有域的调用。您必须联系Web API的所有者并要求他们实现CORS