2013-08-17 94 views
0

我有一些代碼在開發工作正常。訪問控制允許來源getJSON Rails

原代碼

respond_to :json 

def index 
    @tags = current_user.tags 
    respond_with(@tags.map{|tag| {:id => tag.id, :name => tag.name} }) 
end 

get "tags/index" 

和我的jQuery的文件路徑文件中有這樣的

$.getJSON('http://localhost:3000/tags.json', function(data) { 

一行然而,推到當生產我ge發生錯誤 XmlHttpRequest錯誤:Access-Control-Allow-Origin不允許來源。之後做一些研究

Here HereHere

我改變了我的代碼,這

新規範

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 

$.getJSON('http://localhost:3000/tags.json' + '&callback=?', function(data) { 

具有相同的路線。

這是怎麼回事?我仍然得到相同的錯誤,爲什麼沒有回調修復它?

所有jQuery代碼

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: [",", " "] 
     }); 
    }); 

回答

2

嘗試了這一點

,而不是

$.getJSON('http://localhost:3000/tags.json' + '&callback=?', function(data) 

使用本

$.getJSON('/tags.json' + '?callback=?', function(data) 
+0

我這樣做,沒有任何變化,但是這應該SA有未來的錯誤....謝謝! – ebbflowgo

+0

@ebbflowgo錯誤仍然存​​在? –

+0

@ebbflowgo如果您正在生產環境中檢查,請清理並重新編譯您的所有資產。 –