2010-11-22 48 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的數據類型。