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 s开发者_如何学JAVAomething 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?
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.
精彩评论