0

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, 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

2 Answers 2

1

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)
});
Sign up to request clarification or add additional context in comments.

3 Comments

I solved it, apparently I can't use $(this) inside an ajax request while inside a loop : )
@jakobk haven't noticed that, you're "right". You can however use it if you use $.proxy with $.ajax.
I used the ajax request inside an each loop, if I put a parameter with the each loop, and use that parameter later it works fine. Will the proxy function make sure that I am always getting the latest selector / key ?
0
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.

1 Comment

True, typo.. but still if I use an alert,nothing happens

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.