Cross domain XHR with percolator js on node

I have implemented a little API using percolator js CRUDCollection. It is being served by node on localhost:3000. Now I want to be able to use that API with my django app running on localhost:8000, but it fails with error Origin http://localhost:8000 is not allowed by Access-Control-Allow-Origin.

I tried setting the header in the percolator function, but it is being ignored:

list: function($, cb) {
    $.authenticate = checkAuth;
    $.res.setHeader('Access-Control-Allow-Origin', '*');
etc...

I looked into JSONP as an alternative, but couldn't figure out the hooks to wrap the response in the callback function.

How can I solve this?

It's the first time I've heard of Percolator.js. That's an interesting library.

Anyway, setting $.res.setHeader('Access-Control-Allow-Origin', '*'); does work for me. Did you check whether this header is actually present in response?

Now about JSONP. You can use collectionGET instead of list like that:

collectionGET:function($) {
    var data = ...; // some code here

    var data_str = $.json(data).toString( ),
        url = require("url").parse($.req.url, true),
        jsonp = url.query.callback && url.query.callback.length && url.query.callback;

    if (jsonp) {
        $.res.setHeader( "content-type", "application/javascript" );
        $.res.write( jsonp + "(" );
    } else {
        $.res.setHeader( "content-type", "application/json" );
    }
    $.res.write( data_str );
    if (jsonp) {
        $.res.write( ");" );
    }
    $.res.end();
}

It is a bit low level though ( and it seems that Percolator.js adds some additional stuff to the response ). Maybe you need something else then Percolator.js for your task.