Changing data method using javascript doesn't change the way ajax calls users?

I ran into a very strange problem that is hard for me to handle. In my opinion, I have a link with data-remote="true"and data-method="delete". When I click the link, I can see a request DELETEto my rails server. The returned JS code modifies the properties of this link, including hrefand data-method.

When I click this link again, my server receives a request for a new one href, but with the old one data-method, although I changed it from DELETEto POST(it still sends DELETE).

However, if I refresh the page, the HTML is the same as the β€œnew” HTML (modified with my returned JS), but it actually sends the correct request type. That's why the problem puzzles me.

+5
source share
1 answer

Solution found: make sure you use the jQuery Element.data () method to set html data attributes, such as the "data method", etc.

$(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
+9
source

All Articles