Here is my code that I’m including when I load the page:
<script type="text/javascript">
function test() {
$.get('/login', function(data){
alert("test");
$('#flash_content').html("hi");
}, 'script');}
function test2(){
var jqxhr = $.get("/login", function(data) {
$('#flash_content').html(data);}, 'script')
.success(function() { alert("second success"); })
.error(function(request, status,error) { alert(error); })
.complete(function() { alert("complete"); });}
$(document).ready(function() {
$("#login").click(function(event){
event.preventDefault(); // Prevent link from following its href
test2();
});});
</script>
When I call test2(), I can see in my rails server log that it renders the views that it should, and in firebug I see my response from the ajax get is indeed the form I want to overwrite onto the page, however nothing on the page actually changes. I’ve replaced “Data” with the string “hi” and that indeed gets put into the page. As you can see, an alert pops up and tells me :
SyntaxError: missing ; before statement
Here is my controller method for signing in:
@user = User.new
respond_to do |format|
format.html { }
format.js {render :layout => false }
format.xml { head :ok }
end
and the view that should be rendered:
<%= form_tag user_sessions_path, :method => :post do %>
<div class="field">
<%= label_tag :username %><br />
<%= text_field_tag :username %>
</div>
<div class="field">
<%= label_tag :password %><br />
<%= password_field_tag :password %></br>
Remember me : <%= check_box_tag :remember %>
</div>
<div class="actions">
<%= submit_tag "Login" %>
</div>
<% end %>
I’ve been at this for hours, so maybe I’m not seeing something obvious 🙁 Thanks.
You’re telling jquery that what’s being returned is javascript (last parameter to
$.get). This tells jquery that when it gets your response it should evaluate it.However your response is html – trying to evaluate that as javascript doesn’t make sense. You should either be omitting that parameter (letting jquery guess) or set it to ‘html’.
Rails will still use the
format.jsblock for an xhr request, although I believe you’ll need to put it before theformat.htmlversion.