在本地机器上开发时的同源政策

Same origin policy when developing on local machine

本文关键字:开发 机器      更新时间:2023-09-26

我使用Ember和Ember模型来开发一个前端,该前端正在调用Spring/Rest/MongoDB后端,出于开发目的,它都在我的本地机器上运行,但我的调用出现了同源策略错误。

我想知道这方面的常见工作是什么。

这是我的代码:

App = Ember.Application.create();
App.Router.map(function(){
});
App.IndexRoute = Ember.Route.extend({
   model: function(){
       return App.User.find();
   }
});
App.User = Ember.Model.extend({
    lastName: Ember.attr()
});
App.User.adapter = Ember.Adapter.create({
    findAll: function(klass, records) {
        $.getJSONP("http://localhost:8080/users").then(function(data) {
            records.load(klass, data.users);
        });
    }
})

localhost上的同源策略与web的其他部分相同。然而,如果你以文件的形式打开你的网络应用程序(即地址以file:///开头),那么每隔一个uri,甚至是其他文件的uri,都会有不同的来源

要解决这个问题,请从运行在您自己机器上的服务器上为您的应用程序提供服务,而不是通过转到http://localhost来查看它。