由CORS困惑:相同的页面,相同的API控制器,相同的样式代码,不同的结果

时间:2014-09-06 15:00:01

标签: jquery asp.net ajax cors

我正在使用KnockOut&创建一个AspNet Web API应用程序和一个AspNet MVC应用程序。用户界面中的JQuery,我很难正确地进行通信设置。我已经将CORS包安装到Web API中。目前两者都在localhost中运行,我正在使用Chrome进行调试。

一页特别展示了这些问题。在那个页面上,我有一个视图模型,它包含两个不同但结构相同的函数。两者在同一Web API控制器上调用不同但结构相同的API端点。第一个函数loadNewBusiness在服务器上没有任何特殊的CORS配置。第二个生成

XMLHttpRequest cannot load http://localhost:53130/api/transation/getEndorsement/?id=277. No Access-Control-Allow-Origin' header is present on the requested resource. Origin 

Header:
http://localhost:54708' is therefore not allowed access.
Request URL:http://localhost:53130/api/transation/getEndorsement/?id=277
Request Headers
Provisional headers are shown
Accept:*/*
Origin:http://localhost:54708
Referer:http://localhost:54708/Home/Transactions?PolicyId=118
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.103 Safari/537.36
X-DevTools-Emulate-Network-Conditions-Client-Id:D7A23CC9-F076-48A3-A98A-9D92FEFFB3EE
Query String Parametersview sourceview URL encoded
id:277

但是工作调用和非工作调用使用类似的代码命中相同的Web API控制器。第一个电话(newBusiness)有效,第二个电话(认可)没有。

[RoutePrefix("api/transaction")]
    public class TransactionController : ApiController
    {
        private ICommandBus _commandBus;
        public TransactionController(ICommandBus bus)
        {
            _commandBus = bus;
        }

        [Route("getNewBusiness")]
        [HttpGet]
        public NewBusinessData getNewBusiness(int id)
        {
            var data = new GetNewBusinessParameters { Id = id };
            var result = (ICommandResult<NewBusinessData>)_commandBus.Submit<GetNewBusinessParameters>(data);
            return result.Result;

        }

        [Route("getEndorsement")]
        [HttpGet]
        public EndorsementData getEndorsement(int id)
        {
            var data = new GetEndorsementParameters { Id = id };
            var result = (ICommandResult<EndorsementData>)_commandBus.Submit<GetEndorsementParameters>(data);
            return result.Result;

        }
...

安装软件包时添加到WebApiConfig.cs的CORS配置

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

最后,JQuery Ajax调用。第一部作品(newBusiness),第二部作品(认可)没有。

self.loadNewBusiness = function (id) {
        var d = $.Deferred();
        var rdata = {
            id: id
        };
        var xhr = $.get('http://localhost:53130/api/transaction/getNewBusiness/', rdata);
        xhr.done(function (data) {
            self.Id(id);
            self.EffectiveDate(data.EffectiveDate);
            self.BinderIssueDate(data.BinderIssueDate);
            self.BinderExpirationDate(data.BinderExpirationDate);
            self.RetroDate(data.RetroDate);
            self.TailExpirationDate(data.TailExpirationDate);
        });
        return d;
    };

    self.loadEndorsement = function (id) {
        var d = $.Deferred();
        var rdata = {
            id: id
        };
        var xhr = $.get('http://localhost:53130/api/transation/getEndorsement/', rdata);
        xhr.done(function (data) {
            self.Id(id);
            self.EndorsementType(data.EndorsementType);
            self.EffectiveDate(data.EffectiveDate);
            self.Premium(data.Premium);
            self.Description(data.Description);
            self.Text1(data.Text1);
            self.Text2(data.Text2);
            self.Money1(data.Money1);
            self.Money2(data.Money2);
            self.Date1(data.Date1);
            self.Date2(data.Date2);
        });
        return d;
    };

我错过了什么?两个基本相同的函数如何表现不同?

1 个答案:

答案 0 :(得分:0)

尽管出现错误消息,但这并非CORS错误。这是一个简单的网址拼写问题。坏网址有&#34; transaction&#34;拼写错误。 Internet Explorer引发了正确的错误(404),因此如果您处于类似情况,1)不要信任错误消息,2)尝试使用其他浏览器进行调试。我仍然更喜欢Chrome,但在这种情况下IE更好。