首页 \ 问答 \ 在Meteor中启用跨源资源共享?(Enable cross-origin resource sharing in Meteor?)

在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
更新时间:2023-10-08 08:10

最满意答案

您的第二种方法依赖于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 the Include with either Select (for references) or SelectMany (for collections) - something similar to how EF Core processes the Includes 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 

相关问答

更多
  • 您的第二种方法依赖于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 ...
  • 摆脱循环。 那就是问题所在。 您正在向数据库发送大量查询。 将您要搜索的所有电影名称存储在列表中并在那里进行包含。 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 ...
  • 实体框架本身是否决定如何构建有效的查询 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 ...
  • 首先......自上次检查以来,实体框架都有所改进。 表达式.Single(c => c.CompanyId == company.CompanyId)应该由所有帐户失败,因为实体框架应该在Expression.Constant上失败。 我怀疑你实际上已经混淆了你的代码清单。 这有点错误的原因是由于.Single(Expression)工作原理。 与大多数Linq IQueryable扩展方法不同,它会立即进行评估。 using (MyEntities context = new M ...

相关文章

更多

最新问答

更多
  • 延迟socket.io响应,并“警告 - websocket连接无效”(Delayed socket.io response, and “warn - websocket connection invalid”)
  • IIS 7.5仅显示homecontroller(IIS 7.5 only shows homecontroller)
  • 我如何删除ListView中的项目?(How I can remove a item in my ListView?)
  • 武陟会计培训类的学校哪个好点?
  • Spring事务管理:无法解析bean'transactionManager'的引用(Spring transaction management: Cannot resolve reference to bean 'transactionManager')
  • 无法在后面访问链接:焦点,移开,div(Link can't be accessed behind :focus, moved out of the way, div)
  • 总和形式选择值 - 我做错了什么(sum form select values - what am I doing wrong)
  • DocBook5 ISO 690参考书目的文档[关闭](Documentation for DocBook5 ISO 690 bibliography [closed])
  • 为什么改变所有点的颜色而不是一个?(Why changed the color for all points instead of one?)
  • 如何从字符串中删除所有尾部破折号?(How can I remove all trailing dashes from a string?)
  • 有没有办法以这种方式调用这个JavaScript函数?(is there a way to call this javascript function in this way?)
  • 在更换字符串时替换组(Replacing a group while replacing a string)
  • 如何用Dreamweaver制作网页表格
  • git:如何能够再次切换分支(git: how to be able to switch branches again)
  • 应用程序在打印联系人姓名和号码时崩溃(app crashes in the middle of printing contact names and numbers)
  • 用CSS @media查询目标移动设备解决方案的最准确的方法是什么?(what is the most accurate way to query target mobile device resolutions with CSS @media?)
  • Java - 如何仅使元音加倍(Java - How to double only Vowels)
  • 调用root.destroy()后出现“tkinter.TclError:invalid command name”错误(“tkinter.TclError: invalid command name” error after calling root.destroy())
  • 使用键绑定在两个表单之间切换(Switching between two forms with Key Bindings)
  • 我把正确的导入但错误仍然发生在Hibernate 5中的org.hibernate.MappingException(I put the correct import but the error still happen org.hibernate.MappingException in Hibernate 5)
  • 从宁波江北洪塘到宁波南站人力市场乘坐什么车子要转什么车子,我去人力市场考电工上岗证
  • jQuery - 用所有元素的新值替换属性值(jQuery - replace attribute value with new value of all elements)
  • Lenovo Low Profile USB Keyboard 下载后找不到电脑上没有
  • 将反应元素渲染到隐藏区域以进行测量(Render a react element to a hidden area for measurement)
  • 有没有可能确定iOS widget何时隐藏?(Is it possible to determine when iOS widget hides?)
  • 量角器 - 描述没有定义(Protractor - describe is not defined)
  • Jackson序列化:XML和JSON的不同格式(Jackson Serialization: Different formats for XML and JSON)
  • 在Javascfipt中解析XML(Parsing XML in Javascfipt)
  • Firebird客户端安装(Firebird client installation)
  • 在未安装XML DB的情况下从Oracle发送电子邮件(Send email from Oracle without XML DB installed)