CORS发出相同的控制器,一种方法可以,另一种方法则没有

时间:2016-09-14 19:07:27

标签: asp.net angularjs asp.net-web-api cors angular-http

我遇到了非常奇怪的错误。

我在控制器中有两个方法,由angular js http get event调用。

第一个工作正常,第二个工作正在抛出CORS错误,不确定这是怎么可能的,因为它们都在同一个控制器中。

这是我得到的错误:enter image description here

这些是我在angularjs中所做的调用:

\documentclass{article}

% https://tex.stackexchange.com/q/75168/5764
\usepackage{titleref}
\makeatletter
\newcommand*{\currentname}{\TR@currentTitle}
\makeatother

\begin{document}

\section{A section}
Section name: \currentname

\subsection{A subsection}
Subsection name: \currentname

\subsection*{Another subsection}
Subsection name: \currentname

\end{document}

控制器方法:

 $http({
        url: 'http://localhost:52876/api/Admin/GetLoanInfo',
        method: "GET",
        params: { loanID: querystringParam }
    }).success(function (data, status) {
        console.log(data);
        $scope.LoanDetailsVM.LoanStatus = data.LoanStatus;
    }).error(function (data, status) {
        console.log(data);
    });

    $http({
        url: 'http://localhost:52876/api/Admin/GetLoanCovenants',
        method: "GET",
        params: { loanID: querystringParam }
    }).success(function (data, status) {
        console.log(data);
    }).error(function (data, status) {
        console.log(data);
    });

我能够同时使用这两种方法,我在这两种方法中都有断点,但不确定为什么在第一种方法上抱怨CORS。

enter image description here

1 个答案:

答案 0 :(得分:1)

使用来自Web浏览器的CORS调用方法会首先使用OPTIONS请求调用Web API(例如at the end of this article)。

这样,浏览器知道是否可以调用所请求的API。

在您的情况下,对您的端点的调用似乎崩溃,这意味着HTTP 500错误不包含任何CORS标头。

这解释了为什么Web浏览器抱怨CORS HTTP Header丢失:Reason: CORS Header 'Access-Control-Allow-Origin' missing

如果您修复了方法,那么HTTP OPTIONS应该没问题,并且CORS错误就会消失。

相关问题