在Meteor中启用跨源资源共享?(Enable cross-origin resource sharing in Meteor?)
我正在尝试从主应用程序到外部角度应用程序获取通道列表。
我已经将https://github.com/stubailo/meteor-rest/blob/master/packages/rest/README.md添加到我的主流星应用程序中,现在我可以使用json格式的URL获取该集合。
现在,当我尝试从外部角度应用程序的http请求时出现问题。
这是我在主流星应用程序中的内容:
'use strict' Meteor.publish('channels', function (index) { return Channels.find({}); }, { url: 'channels', httpMethod: 'get' });
这是我用来在外部角度应用程序中发出http请求的内容:
// Simple GET request example: $http.get('http://example.com/channels').then(function successCallback(response) { // this callback will be called asynchronously // when the response is available console.log('success'); console.log(response); }, function errorCallback(response) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('error'); console.log(response); });
但我得到的回应是错误:
XMLHttpRequest cannot load http://example.com/channels. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
我该怎么做才能解决这个问题?
I'm trying to get a list of channels from my main application to an external angular app.
I've added https://github.com/stubailo/meteor-rest/blob/master/packages/rest/README.md to my main meteor app and now I can get the collection with a url as json format.
Now the problem comes when I try to http request from the external angular app.
Here's what I have in my main meteor app:
'use strict' Meteor.publish('channels', function (index) { return Channels.find({}); }, { url: 'channels', httpMethod: 'get' });
and here's what I use to make the http request in the external angular app:
// Simple GET request example: $http.get('http://example.com/channels').then(function successCallback(response) { // this callback will be called asynchronously // when the response is available console.log('success'); console.log(response); }, function errorCallback(response) { // called asynchronously if an error occurs // or server returns response with an error status. console.log('error'); console.log(response); });
But what I get in response is an error:
XMLHttpRequest cannot load http://example.com/channels. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.
What can I do to fix this?
原文:https://stackoverflow.com/questions/35492462
最满意答案
您的第二种方法依赖于EF导航属性修复过程。 问题在于每一个
query.Include(q => q.ItemNavN).Load();
声明还将包括所有主记录数据以及相关的实体数据。
使用相同的基本思想,一个潜在的改进可能是对每个导航属性执行一次
Load
,将Include
替换为Select
(对于引用)或SelectMany
(对于集合) - 类似于EF Core如何在内部处理Include
。以第二种方法为例,您可以尝试以下方法并比较性能:
var query = ctx.Filters.Where(x => x.SessionId == id) .Join(ctx.Items, i => i.ItemId, fs => fs.Id, (f, fs) => fs); query.Select(x => x.ItemNav1).Load(); query.Select(x => x.ItemNav2).Load(); query.Select(x => x.ItemNav3).Load(); query.Select(x => x.ItemNav4).Load(); query.Select(x => x.ItemNav5).Load(); query.Select(x => x.ItemNav6).Load(); var result = query.ToList(); // here all the navigation properties should be populated
Your second approach relies on the EF navigation property fixup process. The problem is though that every
query.Include(q => q.ItemNavN).Load();
statement will also include all the master record data along with the related entity data.
Using the same basic idea, one potential improvement could be to execute one
Load
per each navigation property, replacing theInclude
with eitherSelect
(for references) orSelectMany
(for collections) - something similar to how EF Core processes theInclude
s internally.Taking your second approach example, you could try the following and compare the performance:
var query = ctx.Filters.Where(x => x.SessionId == id) .Join(ctx.Items, i => i.ItemId, fs => fs.Id, (f, fs) => fs); query.Select(x => x.ItemNav1).Load(); query.Select(x => x.ItemNav2).Load(); query.Select(x => x.ItemNav3).Load(); query.Select(x => x.ItemNav4).Load(); query.Select(x => x.ItemNav5).Load(); query.Select(x => x.ItemNav6).Load(); var result = query.ToList(); // here all the navigation properties should be populated
相关问答
更多-
实体框架包含性能(Entity Framework Include performance)[2023-09-02]
您的第二种方法依赖于EF导航属性修复过程。 问题在于每一个 query.Include(q => q.ItemNavN).Load(); 声明还将包括所有主记录数据以及相关的实体数据。 使用相同的基本思想,一个潜在的改进可能是对每个导航属性执行一次Load ,将Include替换为Select (对于引用)或SelectMany (对于集合) - 类似于EF Core如何在内部处理Include 。 以第二种方法为例,您可以尝试以下方法并比较性能: var query = ctx.Filters.Wher ... -
我最终以更“手动”的方式使用Entity Framework。 因此,我不得不手动使用一系列dbContext.Database.SqlQuery
("select something from something else")而不是使用dbContext.Set 和许多包含。 经过一些痛苦的编码将所有对象绑定在一起后,我在有问题的机器上测试了它,现在它在所有机器上按预期工作。 所以我不知道它为什么在某些机器上工作而不是其他机器,但是当有很多包含时,EF似乎在某些机器设置上存在问题。 I ended ... -
经过更多的研究,我们发现了一篇来自Pawel Kadluczka的博客文章: Visual Studio代码库中的实体框架代码第一视图生成模板 他创建了一个T4模板 ,可用于从Code First生成视图。 After more research, we found a blog post from Pawel Kadluczka: Entity Framework Code First View Generation Templates On Visual Studio Code Gallery He c ...
-
看一下SQL Server论坛上的这个帖子 。 它有点类似,可能会提供一些线索。 简而言之,您可能在SSMS和ADO.NET中具有不同的SQL Server执行环境选项,从而导致不同的执行计划。 清除SQL Server计划缓存应该有所帮助。 Take a look at this thread on SQL Server forum. It's a bit similar and might give some clues. In short, you may have different SQL Ser ...
-
实体框架性能问题(Entity Framework performance problems)[2022-02-14]
摆脱循环。 那就是问题所在。 您正在向数据库发送大量查询。 将您要搜索的所有电影名称存储在列表中并在那里进行包含。 public async Task- > GetReleaseDatesAsync(List
movieNames) { //movie names that you're searching for - movieNames return await Db.ReleaseDates.Where(x => movieNames ... -
尝试 var count = entities.Post.Where(p => p.SiteID == 1 && p.CreatedDate != null).Query().Count(); http://msdn.microsoft.com/en-us/data/jj574232.aspx 在页面底部有这个: 使用Query来计算相关实体而不加载它们 有时,知道有多少实体与数据库中的另一个实体相关而实际上不会产生加载所有这些实体的成本是有用的。 使用LINQ Count方法的Query ...
-
如果不查看实体的确切形状,请执行以下操作: result = db.MemberWorkEntity.Where(mw => mw.WorkEntity.workEntityLevelID == 2 && mw.Member.C_deleted == null) .Select(s => new MemberDTO { memberNumber = mw.Member.memberNumber, name = mw.Member.name, status = mw.Member ...
-
实体框架性能调优(Entity framework performance tuning)[2020-10-06]
实体框架本身是否决定如何构建有效的查询 EF将始终自动确定如何构建查询,而sql -server也会自动优化查询。 我是否必须使用linq表达式并通过反复试验提高性能? 您可以尝试使用查询,但通常较小的更改不会影响性能(就表达式的排序而言) 您始终可以使用SQL事件探查器来查看EF的功能,并查看查询的效率。 如果需要很长时间,您可以在SSMS中重新运行查询并启用“ 包含实际执行计划”并确定查询速度慢的位置。 does entity framework itself decide on how to buil ... -
DbQuery
.Include(path)文档说明(请在最后阅读NOTES - 它描述了包含工作的路径): 路径包罗万象。 例如,如果包含调用表示包含(“Orders.OrderLines”),则不仅包括OrderLines,还包括Orders。 所以k.Appointment无论如何都会包含k.Appointment 。 即使没有性能损失,第二个查询也更好,因为它不包含重复的包含定义。 更新:数据库查询中没有性能差异,因为两个LINQ查询都会生成相同的SQL查询(嗯,LEFT OUTER JOINS ... -
实体框架Sum()性能(Entity Framework Sum() performance)[2021-11-01]
首先......自上次检查以来,实体框架都有所改进。 表达式.Single(c => c.CompanyId == company.CompanyId)应该由所有帐户失败,因为实体框架应该在Expression.Constant上失败。 我怀疑你实际上已经混淆了你的代码清单。 这有点错误的原因是由于.Single(Expression)工作原理。 与大多数Linq IQueryable 扩展方法不同,它会立即进行评估。 using (MyEntities context = new M ...