2010-11-22 47 views
0

我想使用跨域检查的getJSON()方法我在控制器问题在jQuery.getJSON在导轨2.3.5

def unique_email 
count = User.count(:all, :conditions => ['email = ?',params[:email]]) 
result = Hash.new() 

if count > 0 
    result[:text] = "false" 
else 
result[:text] = "true" 
end 
respond_to do |format| 
    format.json { 
    render :json => result 
    } 
end 

使用以下

<script> jQuery.getJSON("http://localhost:3003/home/unique_email/[email protected]&callback=result", { format: "jsonp" }, function(result) { alert(result.text) }); </script>

结束

我在同一个域中获得警报,这意味着我在我的应用程序中获得了警报,但是在将代码放在应用程序旁边意味着我不是得到任何错误,但应用程序操作被称为..但我没有得到任何来自应用程序的响应。

任何帮助将不胜感激。

感谢&问候,

Ramanavel Selvaraju。

回答

0

我发现这个答案,

文需要添加render_json方法应用控制器。

private 

    # render json, but also allow JSONP and handle that correctly 
    def render_json(json, options={}) 
     callback, variable = params[:callback], params[:variable] 
     logger.debug("render json or jsonp? Callback = #{callback}, Variable=#{variable}") 
     response = begin 
     if callback && variable 
      "var #{variable} = #{json};\n#{callback}(#{variable});" 
     elsif variable 
      "var #{variable} = #{json}" 
     elsif callback 
      "#{callback}(#{json});" 
     else 
      json 
     end 
     end 
     render({:content_type => :js, :text => response}.merge(options)) 
    end 

所以我们可以在jQuery ajax中获得jsonp的数据类型。