简体   繁体   English

成功的ajax获取请求后使用pushState

[英]Using pushState after a successfull ajax get request

I am trying to implement the pushState method, but cannot get it to work properly. 我正在尝试实现pushState方法,但无法使其正常工作。 The history.pushState line gets triggered without performing the desired change of browser window URL. 在未执行所需的浏览器窗口URL更改的情况下触发了history.pushState行。 No error messages are appearing in the console. 控制台中未显示任何错误消息。

The problem is probably the last @href argument, because if I replace it with something like "#test" , the browser window URL changes to /#test . 问题可能是最后一个@href参数,因为如果我将其替换为"#test" ,浏览器窗口URL将更改为/#test

What am I missing here?: 我在这里想念什么?

paramsBtn = $(".reorder-orders")
paramsBtn.off "click"
paramsBtn.click (e) ->
    el = $(e.currentTarget)
    reorder = el.attr("reorder")

    $.ajax
        type: "get"
        dataType: "script"
        data:
            view: reorder
        url: "/api/orders/reorder_orders"
        success: (data, status, xhr) ->
            history.pushState null, "", @href
            false

        error: (xhr, textStatus, errorThrown) ->
            console.log "Error while organizing"

by @href you probably want to refer to the button's href attribute. 通过@href您可能希望引用按钮的href属性。 You cannot access to this anymore in $.ajax 's callback function, so you should store what you need in a variable before. 您无法再通过$.ajax的回调函数访问this函数,因此,您之前应该将所需的内容存储在变量中。

Right after reorder = el.attr("reorder") , add href = @href reorder = el.attr("reorder") ,添加href = @href

Then update your pushState line like this: history.pushState null, "", href 然后像这样更新您的pushState行: history.pushState null, "", href

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM