Node.js – django tastypie and cross domain json

djangoexpressjsonnode.js

Got a django dev server running on localhost:8000 and a nodejs server on localhost:3000. I want to import json into the nodejs server but I'm getting this error:

XMLHttpRequest cannot load
http://127.0.0.1:8000/api/presentation/?format=json. Origin
http://localhost:3000 is not allowed by Access-Control-Allow-Origin

This is my first foray into cross-domain fun so I'm WAY out of my depth.

I've added this to my routes in node (expressjs).

app.all('/', function(req, res){
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.render('index', {
    title: '...'
  });
});

What am I missing / doing wrong?

Best Answer

The data provider needs to set a policy for cross domain request (not the client, as your expressjs snippets suggests).

Someone posted a gist with a simple Django middleware taking care of injecting the needed headers:

Middlware to allow's your django server to respond appropriately to cross domain XHR (postMessage html5 API).