Pencilblue - origin not allowed for controller endpont

67 views Asked by At

I'm developing an API using pencilblue, everything works fine until I make an AJAX request to this endpoint.

I always get an "origin not allowed" error.

Is there a way to enable CORS with pencilblue?

1

There are 1 answers

0
mccannf On

You can do this through creating your own controller:

https://github.com/pencilblue/pencilblue/wiki/Quickstart:-Controllers

And in there add the relevant headers for CORS. E.g. something like:

module.exports = function (pb) {

    //PB dependencies
    var util           = pb.util;
    var BaseController = pb.BaseController;

    /**
     * CORS Controller
     */
    CorsController.prototype.render = function(cb) {

    // Add the CORS Header here
    var output = {
        code: 200,
        headers: {
              'Access-Control-Allow-Origin': '*'
        }            
    };
    this.ts.load('example_api_endpoint', function(error, result) {
        output.content = result;
        cb(output);
    });

   return CorsController;
};