0

I'm curious as to why with this ajax call:

   $.ajax({
      dataType: 'jsonp',
      type: 'GET',
      url: 'http://localhost:3000/gwsearch/ajax_search?d1=36354&d2=36355',
      crossDomain: true
  }).done(function(data){
    alert(data);
  });

To this rails method:

    def ajax_search
     #random code
      respond_to do |format|
        format.html
        format.js { render :json => @gw_search_results.to_json, :callback => params['callback'] }
     end

I am getting an html response. If I remove the "format.html" I get a jsonp response back with the correct data, but I am just curious as to why rails is choosing to send back html by default, instead of jsonp? What am I missing? I thought with a js call, it shoots back a js response if available?

charlietfl
  • 170,828
  • 13
  • 121
  • 150
jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36
  • Did you check out: http://stackoverflow.com/questions/14120937/handling-jsonp-in-rails-3-controller – CDub Nov 21 '13 at 19:15
  • also, read this:- http://guides.rubyonrails.org/layouts_and_rendering.html#rendering-by-default-convention-over-configuration-in-action – Philo Nov 21 '13 at 19:22
  • http://stackoverflow.com/questions/16637522/controller-can-not-detect-ajax-requests – Philo Nov 21 '13 at 19:25

2 Answers2

0

http://davidwparker.com/2010/03/09/api-in-rails-respond-to-and-respond-with/

There needs to be an explicit respond to and respond with statement.

Philo
  • 1,931
  • 12
  • 39
  • 77
  • I have respond_to :html, :json at the top of the controller before any methods and this below at the end of the method, still getting an html response unless I remove format.html. I want to dictate from the request that I want a json response, how can I do this? respond_with do |format| format.html if params[:callback] format.js { render :json => {:items_by_tag => @gw_search_results.to_json}, :callback => params[:callback] } else format.json { render json: {:items_by_tag => @gw_search_results}} end end – jamesdlivesinatree Nov 21 '13 at 21:59
0

Figured it out, for those wondering, the order of your respond_to statements does indeed matter. I did not think they did. This yields me a json response:

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

It seems to hit the jsonp callback conditional and spit me back what I need, where as before, I had

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

I'm guessing it saw the format.html as a valid response and yielded that response instead of the json. Note that even when I changed the request headers on the ajax call to "application/json" I still was getting an html response back with the problematic code I have showcased at the bottom.

jamesdlivesinatree
  • 1,016
  • 3
  • 11
  • 36