nathanvda
nathanvda

Reputation: 50057

rails3: render :json can't be read from jquery?

I have two rails applications. In the first application i have the following method in my controller:

def delivery_status
  envelope_id = params[:id]
  render :json => envelope.to_json(:include => [:deliveries, :log_lines])
end

which, if go to the url in my browser, nicely shows my the JSON. Seems ok. However, if i call this through jQuery, from my second application, using the following:

$(document).ready(function(){
  $('#provisioning_sms_details_dialog').hide();

  $('.get_sms_details').live('click', function(event) {
      var el = $(this),
          data_url = el.attr('data-url');
      $.ajax({
        url: data_url,
        dataType: 'text',
        success: function(data, status, req) {
          alert('received with status' + status)
          alert('received json ' + data);
        }
      });
  });
});

then the right url is hit with the correct parameters but data is always empty. I first tried using $.getJSON, then$.get to end at $.ajax. I am not sure but it seems i am doing something wrong at server-side. If i look at the request inside firebug, the response is indeed empty. Yet, i do not understand, if let the browser hit the same url, i get my json object.

Any insights how to solve this?

Upvotes: 0

Views: 1016

Answers (1)

Magnar
Magnar

Reputation: 28810

Is this a cross domain request? In that case your ajax-request would be of type HEAD, and you would only see an empty body. Ajax calls are restricted by the same origin policy.

If that is the case, you could look into using JSONP, which would solve the problem.

Upvotes: 1

Related Questions