Saturday, June 8, 2013

Angular.js JSONP with Rails

A couple of snippets to remind myself on how to JSONP angular and rails. (cross domain Ajaxing)



-
angular.module('myapp.component', ['ngResource']).factory('MyService', function($resource) {
return $resource('http://giladmanor.com\\:3000/t/t', {alt: 'json', callback: 'JSON_CALLBACK'},
{ 'send': { method: 'JSONP'}});
});
view raw service.js hosted with ❤ by GitHub
class ApiController < ActionController::Base
after_filter :callback_wrapper
def send
render :json=>{:status=>"ok"}
end
private
def callback_wrapper
unless params[:callback].nil?
response.body = "#{params[:callback]}(#{response.body});"
logger.debug "CALLBACK: \n\n #{response.body.inspect}"
end
end
end
view raw service.rb hosted with ❤ by GitHub
-

1 comment:

  1. Your blog is really nice; it has some great information about AngularJS with Rail. Thank you.

    ReplyDelete

Please do not post spam on this blog, Spam sites will be reported to google.
thank you kindly.