2013-01-02 16 views
7

Desidero che l'azione del controller gestisca le richieste jsonp da jquery $.getJSON. Nella mia azione di controllo ho il seguente blocco respond_to:Gestione jsonp in rails 3 controller

respond_to do |format| 
    format.html { render json: {:items_by_tag => @tagged_item_list}} 
    if params[:callback] 
     format.js { render :json => {:items_by_tag => @tagged_item_list}.to_json, :callback => params[:callback] } 
    else 
     format.json { render json: {:items_by_tag => @tagged_item_list}} 
    end 
end 

Ma sto SyntaxError:invalid label quando chiamo l'URL dal $.getJSON. Il mio URL è del formato http://myservice.com?param1=a&param2=b&callback=?.
Qual è il problema con il mio codice che sta causando il fallimento di jsonp?

risposta

19
respond_to do |format| 
    format.html { render json: {:items_by_tag => @tagged_item_list}} 
    if params[:callback] 
    format.js { render :json => {:items_by_tag => @tagged_item_list.to_json}, :callback => params[:callback] } 
    else 
    format.json { render json: {:items_by_tag => @tagged_item_list}} 
    end 
end