我創建了一個AspNet Web API應用程序和一個AspNet MVC應用程序與KnockOut & JQuery在UI中,我很難得到通訊設置正確。我已將CORS軟件包安裝到Web API中。目前,兩者都在本地主機上運行,並且正在使用Chrome進行調試。由CORS困惑:相同的頁面,相同的API控制器,相同的風格代碼,不同的結果
一頁特別說明了這些問題。在該頁面上,我有一個包含兩種不同但結構相同的功能的視圖模型。兩者都在同一個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;
}
...
CORS配置WebApiConfig.cs當封裝安裝
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;
};
我在想什麼?兩個基本相同的函數如何表現不同?