简体   繁体   中英

Django and Ember.js

Alright, so I'm trying to connect Django with ember.js using the django rest framework with the ember-data-django-rest-adapter . The application that I'm using to try this out is the todomvc example provided in the ember.js documentation.

The ember application is currently hitting the server and getting a response (200) but nothing is loaded on the page.

Here is the response from the django server:

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

Here is the contents of that response from curl (note: this response is equivalent to a request for '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
        }
    ]
}

The documentation for the adapter claims "The adapter assumes you have 2 different endpoints per django model." The second is as follows and allows the request of an individual 'todo':

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

I am thinking that the problem lies within my application.js:

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

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

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

If anyone has any ideas, any help would be greatly appreciated! I can post more code if need be. Thanks guys.

EDIT: Debugging. "No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access." Another thread led me to believe CORS might be able to solve this issue. I'll report back.

Don't use PaginationSerializer on the Django side. DjangoRESTAdapter expects an array of items, not an object that describes the page. Relevant quote from ember-data-django-rest-adapter's readme :

iii) Pagination is not yet supported

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM