【问题标题】:Django and Ember.jsDjango 和 Ember.js
【发布时间】:2014-04-03 16:50:50
【问题描述】:

好的,所以我正在尝试使用带有ember-data-django-rest-adapter 的 django rest 框架将 Django 与 ember.js 连接起来。我用来试用的应用程序是 ember.js 文档中提供的todomvc example

ember 应用程序当前正在访问服务器并获得响应 (200),但页面上没有加载任何内容。

这是来自 django 服务器的响应:

[02/Mar/2014 13:51:09] "GET /todos/?_=1393789869335 HTTP/1.1" 200 177

以下是 curl 响应的内容(注意:此响应等同于对 'localhost/todos/' 的请求):

$> curl -H 'Accept: application/json; indent=4' http:/localhost:8080/todos/?_=1393789869335
{
    "count": 2, 
    "next": null, 
    "previous": null, 
    "results": [
        {
            "title": "hmm why isnt this working now", 
            "isCompleted": false
        }, 
        {
            "title": "interesttinnggggg", 
            "isCompleted": false
        }
    ]
}

适配器的文档声称“适配器假定每个 django 模型有 2 个不同的端点。”第二种如下,允许个人'todo'的请求:

$> curl -H 'Accept: application/json; indent=4' http:/localhost:8080/todos/1/
{
    "title": "hmm why isnt this working now", 
    "isCompleted": false
}

我认为问题出在我的 application.js 中:

window.Todos = Ember.Application.create();

Todos.ApplicationAdapter = DS.DjangoRESTAdapter.extend({
  host: 'http://localhost:8080'
 });

 Todos.ApplicationSerializer = DS.DjangoRESTSerializer.extend();

如果有人有任何想法,任何帮助将不胜感激!如果需要,我可以发布更多代码。多谢你们。

编辑:调试。 “请求的资源上不存在‘Access-Control-Allow-Origin’标头。因此,不允许访问原点‘null’。”另一个线程让我相信 CORS 可能能够解决这个问题。我会回来报告的。

【问题讨论】:

    标签: javascript python django ember.js django-rest-framework


    【解决方案1】:

    不要在 Django 端使用PaginationSerializerDjangoRESTAdapter 需要一个项目数组,而不是描述页面的对象。来自 ember-data-django-rest-adapter 的相关引用 readme:

    iii) 尚不支持分页

    【讨论】:

    • 这是问题之一。此外,我必须在 django 的序列化程序中传递一个 ID。以及需要在 django 上将我的主机列入白名单以允许它发出跨域 HTTP 请求。
    猜你喜欢
    • 1970-01-01
    • 2013-02-15
    • 2012-12-22
    • 2012-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-01
    相关资源
    最近更新 更多