【问题标题】:Changing data-method with javascript does not change what method the ajax calls users?用javascript改变数据方法不会改变ajax调用用户的方法吗?
【发布时间】:2012-03-30 01:47:53
【问题描述】:

我遇到了一个非常奇怪的问题,我很难解决。在我看来,我有一个与data-remote="true"data-method="delete" 的链接。当我单击该链接时,我可以看到对我的 rails 服务器的 DELETE 请求。然后返回的 JS 代码改变了这个链接的属性,其中有hrefdata-method

再次单击此链接后,我的服务器收到对新 href 的请求,但使用旧的 data-method,即使我已将其从 DELETE 更改为 POST(它仍然发送 @ 987654330@请求)。

但是,如果我刷新页面,HTML 与“新”HTML 相同(使用我返回的 JS 进行更改),但它实际上发送了正确的请求类型。这就是为什么这个问题让我感到困惑。

【问题讨论】:

  • 同样的问题;找到解决方案后我会更新

标签: ruby-on-rails ruby ujs


【解决方案1】:

找到解决方案:确保使用 jQuery Element.data() 方法设置 html 数据属性,如“数据方法”等。

$(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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-03
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-08
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多