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
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.
What is going on here? I'm still getting the same error, why didn't the callback fix it?
all jQuery Code
var items = [];
var prepopulate = [];
$(function () {
var myVarsJSON = $("#my_vars_json").html(),
myVars = $.parseJSON(myVarsJSON);
$.each(myVars, function(i, obj) {
return prepopulate.push(obj.name);
});
$.getJSON('/tags.json' + '&callback=?', function(data) {
$.each(data, function(i, obj) {
return items.push(obj.name);
});
$("#article_tag_ids_edit").val(prepopulate).select2({
width: "element",
tags: items,
tokenSeparators: [",", " "]
});
});
});
$.getJSON('/tags.json' + '&callback=?', function(data) {
$.each(data, function(i, obj) {
return items.push(obj.name);
});
$("#article_tag_ids").select2({
width: "element",
tags: items,
tokenSeparators: [",", " "]
});
});