在rails 3控制器中处理jsonp [英] Handling jsonp in rails 3 controller
本文介绍了在rails 3控制器中处理jsonp的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我希望我的控制器动作处理来自jquery $。getJSON
的jsonp请求。在我的控制器动作我有以下respond_to块:
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
但我收到 SyntaxError:invalid label
当我调用 $。getJSON
的url。我的网址格式为 http://myservice.com?param1=a¶m2=b&callback=?
。
问题是什么我的代码导致jsonp失败?
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¶m2=b&callback=?
.
What is the problem with my code which is causing jsonp to fail?
推荐答案
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
这篇关于在rails 3控制器中处理jsonp的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文