c# - 使用 Angular 登录 WinApi C#

标签 c# angularjs

我在发送邮件时收到此错误。你知道我必须做什么吗?登录 WinApi C#

XMLHttpRequest cannot load http://localhost:56305/Token. Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64757' is therefore not allowed access. The response had HTTP status code 400.

我的代码

//linki
    $scope.addLink = "http://localhost:56305/Token";

    $scope.addData = function () {
        if ($scope.password != "" && $scope.email) {
            $http({
                method: "POST",
                url: $scope.addLink,
                headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
                headers: {'X-Requested-With' :'XMLHttpRequest'},
                data: { grant_type: 'password', username: $scope.email, Password: $scope.password }
            }).then(function mySucces(response) {
                $scope.stateHideError = true;
                $scope.stateHideSuccess = false;
                Authorization.go('carInsurance');
            }, function myError(response) {
                $scope.stateHideError = false;
                $scope.stateHideSuccess = true;
                $scope.error = "Wystąpił błąd - Problem z wysłaniem parametrów";
            });
        }
        else {
            $scope.stateHideError = false;
            $scope.stateHideSuccess = true;
            $scope.error = "Wystąpił błąd - nie podałeś wszystkich wymaganych wartości.";
        }
    };

PtrScn enter image description here

我想发送这样的内容

enter image description here

我将其添加到 WinApi C#

    <httpProtocol>
        <customHeaders>
          <add name="Access-Control-Allow-Origin" value="*"/>
        </customHeaders>
    </httpProtocol>

现在我遇到了这个错误

XMLHttpRequest cannot load http://localhost:56305/Token. Response for preflight has invalid HTTP status code 400

最佳答案

这是一篇示例文章,如何启用 CORS http://www.asp.net/web-api/overview/security/enabling-cross-origin-requests-in-web-api

您的服务器拒绝来自 js 应用程序的请求,因为您的 js 是从不同的 URL 加载的

关于c# - 使用 Angular 登录 WinApi C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39489689/

相关文章:

c# - 是否有任何默认策略来为决议变更准备申请?

angularjs - 指令 md-whiteframe 和 css 类 md-whiteframe 之间的区别

c# - 我该如何优化这个功能? c# 扫描文本文件中的字符串

c# - asp.net 从 mysql 为用户/ session 分配角色

performance - 嵌套 ng-repeat 性能

javascript - 如何在带有 ES6 的 webpack 中的 Angular 之前包含 jQuery?

javascript - AngularJS 我正在尝试计算嵌套 ng-repeat 中包含的许多输入(ng-model)的结果

javascript - 如何将数据值从服务传递到 Controller

c# - 具有身份和 EF 的 3 层应用程序

c# - 等待函数 需要同步