缺少使用余烬数据的选项请求 [英] Missing options request using ember-data

查看:303
本文介绍了缺少使用余烬数据的选项请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用余烬和余烬数据,但在处理CORS时遇到问题。

I'm trying to get to grips with ember and ember data but having a problem with handling CORS correctly.

我已经设法定义模型等一个静态夹具,但现在想使用一些远程JSON。所以我设置ember-data像这样:

I've managed to define a model etc using a static fixture but now want to use some remote JSON. So I setup ember-data like this:

App = Ember.Application.create();

App.Store = DS.Store.extend({
    revision: 13,
    adapter: DS.RESTAdapter.create({
        url: 'http://clara.eagle/v1/money'
    })
});

这样的模型:

App.Transaction = DS.Model.extend({
    type:       DS.attr('string'),
    occurrence: DS.attr('date'),
    details:    DS.attr('string'),
    amount:     DS.attr('number'),
    currency:   DS.attr('string') 
});

这样的路线:

App.IndexRoute = Ember.Route.extend({
    model: function() {
        return App.Transaction.find();
    }
});

作为后端我有一个现有的API,将返回一个有效的 GET 请求以下CORS标头 OPTIONS 请求。

As a backend I have an existing API that will return JSON for a valid GET request the following CORS headers an OPTIONS request.

Access-Control-Allow-Origin: http://ember.eagle
Access-Control-Allow-Headers: X-Requested-With, X-AUTHENTICATION, X-IP
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS

当我运行应用程序时,chrome告诉我:

When I run the app however chrome tells me:

XMLHttpRequest无法加载http://clara.eagle/v1/money/transactions。原始http://ember.eagle不被Access-Control-Allow-Origin允许。

看看 OPTIONS 请求给出了什么结果,除了我可以找到一个。虽然这解释了为什么API请求失败,我不知道为什么 OPTIONS 请求没有被执行,因为它最终使用jQuery做出请求(我理解它)。

So I looked at the network tab to see what result the OPTIONS request gave, except I could find one. Whilst this explains why the API request was failing I don't know why the OPTIONS request isn't being executed, since its ultimately making the request using jQuery (as I understand it).

我的问题是为什么这个 OPTIONS 请求没有生成?

My question therefore is why is this OPTIONS request not being generated? and if it's not designed to then how do I make it do so?

我已经测试过 OPTIONS 请求是由API生成的,并且 GET 请求也可以工作,所以我不相信API是有故障的(截图)。使用自己的jQuery(即vanilla jQuery), OPTIONS 请求按预期运行。

I have tested that the OPTIONS request is generated by the API, and that the GET request also works so I don't believe the API is at fault (screenshot). Using a jQuery on its own (i.e. vanilla jQuery) the OPTIONS request is run as expected.

到Ember os可能有一些我缺少,但是目前我看不到它。

I am new to Ember os there's probably something i'm missing but at the moment I can't see it!

推荐答案

两个变化:

App.Store = DS.Store.extend({
  revision: 13,
  adapter: DS.RESTAdapter.create({
    url: 'http://clara.eagle/v1/money',
    corsWithCredentials: true
  })
});

并将此添加到您的服务器配置 Access-Control-Allow-Credentials :true 这与ajax设置的 corsWithCredentials 选项一起使用:

And additionally add this to your server configuration Access-Control-Allow-Credentials: true this goes in conjunction with the corsWithCredentials option for the ajax setup:

Access-Control-Allow-Origin: http://ember.eagle
Access-Control-Allow-Headers: X-Requested-With, X-AUTHENTICATION, X-IP
Access-Control-Allow-Credentials: true
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS

希望它有帮助。

这篇关于缺少使用余烬数据的选项请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆