Accepting Cross Domain Requests In Sails.Js Builtin Rest API & Custom Controller Actions

In Sails.Js By Default the cross-domain API calls are restricted but you can enable/accept JSONP calls by changing jsonp: true in config/controllers.js file.Now you are free to make the built in Sails.js API calls from any other server to your Sails.js App.

Now you have solved this issue for the Sails.JS BuiltIn REST API but you still gonna face this same problem for your custom controller actions written inside Sails.Js But you can easily resolve this by editing allRoutes to “true”(by default it will be false) in file: config/core.js

This change is gonna affect for all of your controller actions in your sails.js app and if in case you dont want that instead you just want to allow cross domain requests for one or two actions means you achieve that by overriding this settings inside file config/routes.js like below

                         cors: {
                                    origin: ‘*’   

In the above example i have given the origin:’*’ that means for any other domains but you specify one or more domains as comma separated.

Note:JSONP not gonna work for the above change too but normal AJAX call from your Javascript will work for sure.


origin: ",",

I would recommend a wiki here: about core.js in sailsJS for more details..

JSONP call Example:


Normal http call example:

$http({method: 'GET', url: 'http://localhost:1337/user/count?callback=JSON_CALLBACK'}).
             success(function(data, status, headers, config) {
           // this callback will be called asynchronously
           // when the response is available
             error(function(data, status, headers, config) {
           // called asynchronously if an error occurs
           // or server returns response with an error status.

Hope this may Help Someone.

Sirin K

Leave a Reply

Your email address will not be published. Required fields are marked *