简体   繁体   English

HTTP错误405.0-从angularjs应用访问web-api时不允许使用方法

[英]HTTP Error 405.0 - Method Not Allowed while accessing web-api from angularjs app

在此处输入图片说明 I am creating a anjular js app where i have created a login page and im authenticating it using web api. 我正在创建一个Anjular js应用程序,其中已创建一个登录页面并使用Web API对其进行即时身份验证。 This is the controller which is returning json and it is running properly. 这是返回json且正在正常运行的控制器。

[Route("api/Customer/{id}/{pass}")]
public IEnumerable GetCustomer(string id, string pass)
{
        var list = repository.Get(id, pass);

        if (list != null)
        {
            return list;
        }
        else
        {
            return null;
        }
}

this is the controller accessing the api in angular app (demoApi.js) 这是控制器在角度应用程序(demoApi.js)中访问api

(function () {
    'use strict';
    var DemoApi = function ($http) {
    var loginCheck = function (username, password) {
        return $http.get("http://localhost:8115/api/Customer/" +username+"/"+password)
            .then(function (response) {
                return response.data;
            });
        };
        return {
             loginCheck: loginCheck
        }
    };

    var app = angular.module("angularApp");
    app.factory("DemoApi", DemoApi);

}());

this is the login controller(loginController.js) 这是登录控制器(loginController.js)

(function () {
var app = angular.module("angularApp");

var loginController = function ($scope, $http, bankApi, $location) {
    var userDetails = {
        username: $scope.uName,
        password: $scope.uPassword
    };

    $scope.login = function () {
        DemoApi.loginCheck(userDetails.username, userDetails.password)
            .then(onLogin, onError);
    };

    var onError = function (reason) {
        $scope.error = "Could not fetch the data.";

    };
    var onLogin = function (data) {
        $scope.details = data;
        $location.path("/info");
    };
}

app.controller('loginController', loginController);
}());

this is the index page where i have all the scripts linked 这是我链接了所有脚本的索引页

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" ng-app="angularApp">
    <head>
   <title>Ebank</title>
   <script src="Scripts/angular.js"></script>
   <script src="Scripts/angular-route.js"></script>
   <link href="Content/bootstrap.min.css" rel="stylesheet" />
   <script src="lib/app.js"></script>
   <script src="lib/bankApi.js"></script>   
   <script src="lib/loginController.js"></script>
   <script src="lib/infoController.js"></script>
   </head>
   <body style="background-color:#333">
   <div ng-view></div>
   </body>
</html>

Provide me with a solution, why im not able to call api from demoApi.js I'm getting the response if i call this api directly from the html page but not getting response from demoApi.js 给我提供一个解决方案,为什么我不能从demoApi.js调用api如果我直接从html页面调用此api但没有从demoApi.js接收响应,为什么我得到响应

Most probably you bumped into CORS issue. 很可能您遇到了CORS问题。 You need to enable CORS. 您需要启用CORS。 Follow below mentioned steps. 请遵循以下提到的步骤。

  1. Open IISExpress config file applicationhost.config located at C:\\Program Files (x86)\\IIS Express\\AppServer. 打开位于C:\\ Program Files(x86)\\ IIS Express \\ AppServer上的IISExpress配置文件applicationhost.config。
  2. Search for "httpProtocol" tag. 搜索“ httpProtocol”标签。 Inside it check if "customHeaders" tag exists. 在其中检查“ customHeaders”标签是否存在。 If not then add it. 如果没有,则添加它。
  3. Add following two tag inside "customHeaders" tag. 在“ customHeaders”标签内添加以下两个标签。

<add name="Access-Control-Allow-Origin" value="*" /> <add name="Access-Control-Allow-Headers" value="Content-Type" />

Alternatively you can run chorme browser in security disabled mode using following command on command prompt and try accessing your site from there. 或者,您可以在命令提示符下使用以下命令以禁用安全性的模式运行chorme浏览器,然后尝试从那里访问您的网站。

Full path to chrome.exe --disable-web-security --user-data-dir chrome.exe的完整路径--disable-web-security --user-data-dir

Note its just workaround to test your application however in production deployment you need to enable CORS. 请注意,它只是测试您的应用程序的解决方法,但是在生产部署中,您需要启用CORS。

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

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