Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want my controller action to handle jsonp requests from jquery $.getJSON. In my controller action i have the following respond_to block:

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

But I'm getting SyntaxError:invalid label when i call the url from $.getJSON. My url is of the form http://myservice.com?param1=a&param2=b&callback=?.
What is the problem with my code which is causing jsonp to fail?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
229 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...