gpt4 book ai didi

javascript - Breeze.js 到 ODATA Web API - CORS 请求失败

转载 作者:行者123 更新时间:2023-12-02 17:22:11 25 4
gpt4 key购买 nike

我正在尝试针对基于 Microsoft Web API 2 构建的 ODATA 服务使用 Breeze.js 查询。

当 UI 和 ODATA Controller 位于同一个 Web 应用程序中时,一切都按预期运行。

然后我决定将我的数据服务和 Web 应用程序放入单独的项目中。我安装了所需的 CORS 包。当我从 LinqPad 发送查询时,我的数据服务功能齐全。

但是,Breeze 无法解析到达的响应负载。

这是我的相关控制台跟踪:

XHR finished loading: GET "http://localhost:63494/odata/$metadata". (index):1
XHR finished loading: GET "http://localhost:63494/odata/Positions?$filter=(PeriodEndDate%20eq%20dateti…ue%2CCustodianAccountCode%2CCustodianCode%2CSubBusinessUnit%2CStrategyCode".
[Q] Unhandled rejection reasons (should be empty):
["Error↵ at createError (http://localhost:57299/S…/localhost:57299/Scripts/datajs-1.1.3.js:2587:25)"]
q.js:989

在调试器中,我可以看到带有数据的响应从服务器到达,但 Breeze 无法从 json 中解析数据。

我的环境:

在 ODATA 服务器端,我安装了以下相关软件包:

<package id="Breeze.EdmBuilder" version="1.0.5" targetFramework="net45" />
<package id="Breeze.Server.ContextProvider" version="1.4.12" targetFramework="net45" />
<package id="Breeze.Server.ContextProvider.EF6" version="1.4.12" targetFramework="net45" />
<package id="Breeze.Server.WebApi2" version="1.4.12" targetFramework="net45" />
<package id="EntityFramework" version="6.1.0" targetFramework="net45" />
<package id="jQuery" version="1.8.2" targetFramework="net40" />
<package id="jQuery.UI.Combined" version="1.8.24" targetFramework="net40" />
<package id="knockoutjs" version="2.2.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Cors" version="5.2.0-rtm-140522" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc" version="5.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.OData" version="5.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.Providers.Core" version="1.2" targetFramework="net40" />
<package id="Microsoft.AspNet.Providers.LocalDB" version="1.1" targetFramework="net40" />
<package id="Microsoft.AspNet.Razor" version="3.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0-rtm-140522" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.0-rtm-140522" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Cors" version="5.2.0-rtm-140522" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Tracing" version="5.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.0-rtm-140521" targetFramework="net45" />
<package id="Microsoft.Data.Edm" version="5.6.1" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.1" targetFramework="net45" />
<package id="Microsoft.OData.Core" version="6.3.0" targetFramework="net45" />
<package id="Microsoft.OData.Edm" version="6.3.0" targetFramework="net45" />
<package id="Microsoft.Spatial" version="6.3.0" targetFramework="net45" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
<package id="System.Spatial" version="5.6.1" targetFramework="net45" />
<package id="WebActivator" version="1.5.3" targetFramework="net45" />

在网络应用程序(ODATA 客户端)端,我有:

<package id="Antlr" version="3.5.0.2" targetFramework="net45" />
<package id="Breeze.Client" version="1.4.12" targetFramework="net45" />
<package id="Breeze.Server.ContextProvider" version="1.4.12" targetFramework="net45" />
<package id="Breeze.Server.WebApi2" version="1.4.12" targetFramework="net45" />
<package id="datajs" version="1.1.3" targetFramework="net40" />
<package id="DotNetOpenAuth.AspNet" version="4.3.4.13329" targetFramework="net40" requireReinstallation="True" />
<package id="DotNetOpenAuth.Core" version="4.3.4.13329" targetFramework="net40" requireReinstallation="True" />
<package id="DotNetOpenAuth.OAuth.Consumer" version="4.3.4.13329" targetFramework="net40" requireReinstallation="True" />
<package id="DotNetOpenAuth.OAuth.Core" version="4.3.4.13329" targetFramework="net40" requireReinstallation="True" />
<package id="DotNetOpenAuth.OpenId.Core" version="4.3.4.13329" targetFramework="net40" requireReinstallation="True" />
<package id="DotNetOpenAuth.OpenId.RelyingParty" version="4.3.4.13329" targetFramework="net40" requireReinstallation="True" />
<package id="EntityFramework" version="6.1.1-beta1" targetFramework="net45" />
<package id="jQuery" version="2.1.1" targetFramework="net40" />
<package id="jquery.event.drag" version="2.2.0" targetFramework="net40" />
<package id="jQuery.UI.Combined" version="1.10.4" targetFramework="net40" />
<package id="jQuery.Validation" version="1.12.0" targetFramework="net40" />
<package id="knockoutjs" version="3.1.0" targetFramework="net40" />
<package id="Microsoft.AspNet.Mvc" version="5.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.Mvc.FixedDisplayModes" version="5.0.0" targetFramework="net45" />
<package id="Microsoft.AspNet.Razor" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.Web.Optimization" version="1.1.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi" version="5.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.OData" version="5.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages.Data" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages.OAuth" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.AspNet.WebPages.WebData" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.Bcl" version="1.1.9" targetFramework="net40" requireReinstallation="True" />
<package id="Microsoft.Bcl.Build" version="1.0.14" targetFramework="net40" />
<package id="Microsoft.Data.Edm" version="5.6.1" targetFramework="net45" />
<package id="Microsoft.Data.OData" version="5.6.1" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Ajax" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.jQuery.Unobtrusive.Validation" version="3.2.0-rtm-140523" targetFramework="net45" />
<package id="Microsoft.Net.Http" version="2.2.22" targetFramework="net40" requireReinstallation="True" />
<package id="Microsoft.Web.Infrastructure" version="1.0.0.0" targetFramework="net40" />
<package id="Modernizr" version="2.7.2" targetFramework="net45" />
<package id="Newtonsoft.Json" version="6.0.3" targetFramework="net45" />
<package id="Q" version="1.0.0" targetFramework="net45" />
<package id="SlickGrid" version="2.2.20140120" targetFramework="net40" />
<package id="System.Spatial" version="5.6.1" targetFramework="net45" />
<package id="WebActivator" version="1.5.3" targetFramework="net45" />
<package id="WebGrease" version="1.6.0" targetFramework="net45" />

我在 ODATA 端的 Controller 如下所示:

public class PositionsController : ODataController
{
private AG_DWEntities db = new AG_DWEntities();

public PositionsController()
{
db.Database.Log = s => Debug.WriteLine(s);
}

// GET odata/Positions
[EnableQuery]
public IQueryable<Position> GetPositions()
{
return db.Positions;
}

// GET odata/Positions(5)
[EnableQuery]
public SingleResult<Position> GetPosition([FromODataUri] string key)
{
return SingleResult.Create(db.Positions.Where(position => position.CCY == key));
}

我在数据服务器端使用 Breeze 的 EdmBuilder(而不是 ODataConventionModelBuilder):

private static IEdmModel GetEdmModelModelForBreeze()
{
return EdmBuilder.GetEdm<AG_DWEntities>();
}

CORS 的启用方式是:

config.EnableCors(new EnableCorsAttribute("*", "*", "*", "*"){ SupportsCredentials = true, PreflightMaxAge = long.MaxValue});

在 Web 应用程序中,查询如下所示:

 var serverAddress = "http://localhost:63494/odata/";

breeze.config.initializeAdapterInstances({ dataService: "webApiOData" });
var manager = new breeze.EntityManager(serverAddress);
my.vm = {
positions: ko.observableArray([]),
load: function () {
var query = breeze.EntityQuery
.from("Positions")
.where("PeriodEndDate", "==", new Date(Date.UTC(2014, 0, 30, 0, 0, 0, 0)))
.where("Portfolio", "==", "SUPR")
.take(200)
.orderBy("AGPositionKeyId")
.select("ID,Portfolio,AGPositionKeyId,BookEndingMarketValue,CustodianAccountCode,CustodianCode,SubBusinessUnit,StrategyCode");
try {
manager.executeQuery(query)
.then(function(data) {
$.each(data.results, function(i, c) {
my.vm.positions.push(c);
})
.fail(function(e) {
alert(e);
});
});
} catch (e) {
alert(e);
};
},
}
my.vm.load();
ko.applyBindings(my.vm);

我见过this post ,并尝试从 ApiController 继承我的 Controller 并使用 Breeze 属性,但它也不起作用 - 我开始收到不同的服务器错误。

感谢您阅读我的长问题!

最佳答案

这个answer为我的问题提供了解决方案。

来自 WebApiConfig.cs:

//THIS DOESN'T WORK:
//var cors = new EnableCorsAttribute("*", "*", "*", null) { SupportsCredentials = true };
//NEITHER DOES THIS:
//var cors = new EnableCorsAttribute("*", "*", "*", "*") { SupportsCredentials = true };

//THIS WORKS:
var cors = new EnableCorsAttribute("*","*","*","DataServiceVersion, MaxDataServiceVersion"){SupportsCredentials = true};

config.EnableCors(cors);

datajs 需要这些响应 header :another discussion .

关于javascript - Breeze.js 到 ODATA Web API - CORS 请求失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23839897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com