简体   繁体   English

状态200 OK(),角度承诺引发错误—否'Access-Control-Allow-Origin'

[英]Status 200 OK(), angular promise throws error — No 'Access-Control-Allow-Origin'

First of all, cors is enabled and working in webapi, i can confirm this by doing a post/get request from another controller without problems, whenever is disable cors i can no longer post/get data from the server. 首先,启用了cors并在webapi中工作,我可以通过从其他控制器发出发布/获取请求而不会出现问题来确认这一点,每当禁用cors时,我将无法再从服务器发布/获取数据。 This is also true for the token, as soon as i disable the cors on the webapi i do not get a valid response. 对于令牌来说也是如此,一旦我在webapi上禁用了cors,我就不会得到有效的响应。 But for now i do get a valid response. 但是现在我确实得到了有效的答复。

I'm trying to get a token from the web API. 我正在尝试从Web API获取令牌。 I use the angular resource and a promise. 我使用有角度的资源和承诺。 In fiddler i can see status 200, so i'm getting a token back, the JSON says bearer token = x so everything seems fine. 在小提琴手中,我可以看到状态200,因此我得到了令牌,JSON表示不记名令牌= x,所以一切似乎都很好。 Postman is also working. 邮递员也在工作。 But when i look into the debug console, i see that the promise is throwing an error, even tho fiddler and postman both show me a valid JSON file and a status 200. 但是,当我查看调试控制台时,我看到诺言抛出了一个错误,甚至提琴手和邮递员都向我展示了一个有效的JSON文件和状态200。

// Controller
.controller('LoginCtrl', ['$scope', 'Token', function ($scope, Token) {
    $scope.login = function () {Token.save('username=test123@test.com&password=Test123!!&grant_type=password')
            .$promise.then(function (response) {
            console.log(response);
        });
    };
}]);

And in my factory 而在我的工厂

.factory('Token', ['$resource', function ($resource) {
    return $resource(baseUrl + 'token', null, {
        save: {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
                'Accept': '*/*'
            },
        }
    });
}]);

the response i get in Developer tools console is 我在开发人员工具控制台中得到的响应是

Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transformRequest":[null],"transformResponse":[null],"jsonpCallbackParam":"callback","headers":{"Content-Type":"application/x-www-form-urlencoded","Accept":" / "},"data":"username=test123@test.com&password=Test123!!&grant_type=password","url":" http://localhost:55126/token "},"statusText":""} 可能未处理的拒绝:{“ data”:null,“ status”:-1,“ config”:{“ method”:“ POST”,“ transformRequest”:[null],“ transformResponse”:[null],“ jsonpCallbackParam” :“回调”,“标题”:{“内容类型”:“ application / x-www-form-urlencoded”,“接受”:“ / ”},“数据”:“用户名=test123@test.com&password= Test123 !!&grant_type = password“,” url“:” http:// localhost:55126 / token “},” statusText“:”“}

I'm 100% sure i'm getting a valid JSON object from the /token call. 我100%确定我从/ token调用中获取了有效的JSON对象。 Thanks in advance. 提前致谢。


Update 更新资料

XMLHttpRequest cannot load localhost:55126/token. XMLHttpRequest无法加载localhost:55126 / token。 No 'Access-Control-Allow-Origin' header is present on the requested resource. 所请求的资源上没有“ Access-Control-Allow-Origin”标头。 Origin 'localhost:9000'; 来源'localhost:9000'; is therefore not allowed access. 因此,不允许访问。 angular.js:14642 – angular.js:14642 –

Possibly unhandled rejection: {"data":null,"status":-1,"config":{"method":"POST","transfor‌​mRequest":[null],"tr‌​ansformResponse":[nu‌​ll],"jsonpCallbackPa‌​ram":"callback","hea‌​ders":{"Content-Type‌​":"application/x-www‌​-form-urlencoded","A‌​ccept":"/"},"data":"‌​username=test123@tes‌​t.com&password=Test1‌​23!!&grant_type=pass‌​word","url":"localho‌​st:55126/token";‌​},"statusText":""} 可能未处理的拒绝:{“ data”:null,“ status”:-1,“ config”:{“ method”:“ POST”,“ transfor‌mRequest”:[null],“ tr‌ansformResponse”:[nu‌ ll],“ jsonpCallbackPa‌ram”:“回调”,“ headers”:{“ Content-Type‌”:“ application / x-www--form-urlencoded”,“ A‌ccept”:“ /”} ,“ data”:“ ‌username=test123@tes‌t.com&password=Test1‌23 !!&grant_type = password”,“ url”:“ localho‌st:55126 / token”; ‌},“ statusText “:”“}

These 2 error lines pop up just after each other 这两条错误线彼此紧接着弹出

The solution seems te be CORS related. 解决方案似乎与CORS有关。 Even tho i installed NuGet Microsoft.AspNet.WebApi.Cors and used: 我甚至安装了NuGet Microsoft.AspNet.WebApi.Cors并使用:

config.EnableCors(new EnableCorsAttribute(" ", " ", "*")); config.EnableCors(new EnableCorsAttribute(“ ”,“ ”,“ *”));

This helped for the 'normal' controller request, whenever requesting a token i found out that i had to install NuGet Microsoft.Owin.Cors and use: 每当请求令牌时,我发现我必须安装NuGet Microsoft.Owin.Cors并使用:

app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

In the web api config section of the app. 在应用程序的Web API配置部分中。 Thanks for helping. 感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM