Changing data-method with javascript does not change what method the ajax calls users?
Asked Answered
S

1

5

I've run into a very strange issue that I'm having a tough time fixing. In my view, I have a link with data-remote="true" and data-method="delete". When I click the link, I can see a DELETE request to my rails server. The JS code returned then changes this link's properties, among which are the href and data-method.

Upon clicking this link again, my server is receiving a request to the new href, but with the old data-method, even though I have changed it from DELETE to POST (it still sends a DELETE request).

If I refresh the page, however, the HTML is the same as the "new" HTML (changed with my returned JS), but it actually sends the right request type. This is why the issue is puzzling me.

Sato answered 30/3, 2012 at 1:47 Comment(1)
same problem here; i'll update once i find a solutionBawcock
B
10

Found a solution: Make sure you use the jQuery Element.data() Method for setting html data-attributes like "data-method" and so forth.

$(this).data('method', 'post'); # sets "data-method" attribute to "post"
$(this).data('method', 'delete'); # sets "data-method" attribute to "delete"

# "this" refers to the clicked link element for example
Bawcock answered 2/6, 2012 at 12:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.