I have a Rails application where I have some ajax calls, it usally works just fine, but this one I can get working:
I tried running the rails class from the browser开发者_C百科, works fine, and I tried calling an alert with the params just before the ajax call, works fine.. but no connection, whats wrong?
jQuery:
$.ajax({
url: "things/ajax_text_read",
data: "book_id=" + book_id + "&page_number=" + page_number + "&type="+ type,
success: function(){$(this).text('data'); }
});
Rails:
def ajax_text_read
@book_id = params['book_id']
@page_number = params['page_number']
@type = params['type']
@text = Thing.where(:parent_id => @page_number, :book => @book_id, :media_type => 'text')
if(@type=='description')
render :inline => "<%= @text[0].description %>"
else
render :inline => "<%= @text[0].title %>"
end
end
Supposing that things/ajax_text_read
is a valid uri :
$.ajax({
url: "things/ajax_text_read",
data: "book_id=" + book_id + "&page_number=" + page_number + "&type="+ type,
success: $.proxy(function(data){
$(this).text(data);
}, this)
});
success: function(){$(this).text('data'); }
doesn't make much sense. $(this) in this context is going to be the success function, and you're trying to set its text content to the word 'data'?
Perhaps you mean this:
success: function(data) { $('#something_in_your_page').text(data); }
instead.
精彩评论