Access-Control-Allow-Origin getJSON Rails
I have some code that works fine in development.
ORIGINAL CODE
respond_to :json
def index
@tags = current_user.tags
respond_with(@tags.map{|tag| {:id => tag.id, :name => tag.name} })
end
with a route file of
get "tags/index"
and my jQuery file has a line like this
$.getJSON('http://localhost:3000/tags.json', function(data) {
However, when pushing it to production I get an error XmlHttpRequest
error: Origin is not allowed by Access-Control-Allow-Origin. After doing
some research
Here Here and Here
I changed my code to this
NEW CODE
def index
@tags = current_user.tags
respond_to do |format|
if params[:callback]
format.json { render :json => @tags.map{|tag| {:id => tag.id, :name
=> tag.name} }.to_json , :callback => params[:callback]}
else
format.json { render :json => @tags.map{|tag| {:id => tag.id, :name
=> tag.name} }}
end
end
end
and
$.getJSON('http://localhost:3000/tags.json' + '&callback=?', function(data) {
with the same route.
I was able to get rid of the XmlHttpRequest error, however I am now
looking at this error "Failed to Load Resource tags.json"
What is going on here?
No comments:
Post a Comment