Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

include HTTP method in Javascript redirect?

In my layout, I have a devise sign in / sign out link, like so:

=if user_signed_in? then link_to "_", destroy_user_session_path, :method => :delete else link_to "_", new_user_session_path, :method => :get end

This uses the rails helpers to build up the link, and resolves to the following HTML:

<a data-method="get" href="/users/sign_in">_</a>

I'm converting all links to buttons, and have just passed in URLs to onClick functions to redirect the browser. In this case, I don't think a simple redirect will do the trick, because I need to specify the HTTP method. Is this the right way to do this, and if so, how do I tell Javascript about the HTTP method?

Thank you

like image 427
Walrus the Cat Avatar asked Nov 18 '25 04:11

Walrus the Cat


1 Answers

Ok, I tried XMLHttpRequest, but couldn't get it to work. I ended up doing this, which is kind of hacktastic, but it works:

login = function(url) {
$.ajax({
    url: url,
    type: "GET"
}).done(function(){
    window.location.href = url; 
});

}

logout = function(url) {
$.ajax({
    url: url,
    type: "DELETE"
}).done(function(){
    window.location.href = "/";
});
like image 159
Walrus the Cat Avatar answered Nov 20 '25 17:11

Walrus the Cat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!