CORS发出相同的控制器,一个方法是确定,其他一个不是 [英] CORS Issue same controller, one method is ok, other one is not

查看:319
本文介绍了CORS发出相同的控制器,一个方法是确定,其他一个不是的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了很奇怪的错误。

Very strange error I'm experiencing.

我有两个方法在控制器中被angular js http get事件调用。

I have two methods in controller which are called by angular js http get event.

第一个工作正常,第二个抛出CORS错误,不知道如何可能,因为它们都在同一个控制器中。

First one works fine, second one is throwing CORS error, not sure how is that possible since both of them are in same controller.

这是我得到的错误:

这些是我的电话在angularjs中执行:

These are the calls I'm doing in angularjs:

 $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);
    });

而控制器方法:

[HttpGet]
[Route("api/Admin/GetLoanInfo")]
public async Task<IHttpActionResult> GetLoanInfo(int loanID)
{

        LoanApplication newApplication = null;
        newApplication = db.LoanApplications.FirstOrDefault(s => s.LoanId == loanID);
        return Ok(newApplication);
}


[HttpGet]
[Route("api/Admin/GetLoanCovenants")]
public async Task<IHttpActionResult> GetLoanCovenants(int loanID)
{
        LoanCovenant newCovenant = null;
        newCovenant = db.LoanCovenants.FirstOrDefault(s => s.LoanID == loanID);
        return Ok(newCovenant);
}



我可以打这两种方法,我有两个断点方法,但不知道为什么在第一个投诉CORS。

I'm able to hit both methods, I have breakpoints in both of the methods, but not sure why is complaining about CORS on the first one.

推荐答案

从Web浏览器调用方法使用CORS,首先使用 OPTIONS 请求调用Web API(示例)。

Calling methods using CORS from a Web browser makes Web API being called first with an OPTIONS request (example at the end of this article).

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

This way, the browser knows if it can call the requested API.

在您的情况下,对您的端点的调用似乎崩溃,这意味着HTTP 500错误

In your case, the call to your endpoint seems to be crashing, which means the HTTP 500 error does not contain any CORS headers.

这解释了为什么Web浏览器违反CORS HTTP标头缺少:原因:CORS标题的访问控制允许

This explains why the web browser complaning about CORS HTTP Header missing: Reason: CORS Header 'Access-Control-Allow-Origin' missing.

如果您修复了方法,则 HTTP OPTIONS 确定,CORS错误会消失。

If you fix your method, then HTTP OPTIONS should be ok, and the CORS erros would go away.

这篇关于CORS发出相同的控制器,一个方法是确定,其他一个不是的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆