1

我正在使用Ratchet 2构建应用程序。我正在尝试进行简单的 ajax 调用并得到这样的响应

function tryLogin() {
var hxr;

if (window.XMLHttpRequest) {
    xhr = new XMLHttpRequest();
} else {
    xhr = new ActiveXObject("Microsoft.XMLHTTP");
}

xhr.open("POST", "absolutePathToMyScript");
xhr.onreadystatechange = function() {
    if (xhr.readyState == 4) {
        var obj = JSON.parse(xhr.responseText);
        console.log(obj);
        //spinner.classList.remove("active");
    }
}

var data = {
    login: document.querySelector('#loginForm input[name="login"]').value,
    pwd: document.querySelector('#loginForm input[name="pwd"]').value
};

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("login=" + data.login + "&pwd=" + data.pwd);

}

当我单击触发tryLogin()ajax 调用的按钮时,该按钮执行良好,但页面已重新加载。我假设它来自 push.js

有什么解决方案可以忽略 ajax 调用中的 push.js 吗?

4

2 回答 2

0

Have you tried putting the data-ignore="PUSH" attribute on the button that is firing your javascript event? Seems like that should work, you may also be able to look at PUSH.js and just toggle that options off for your request. You can get the PUSH object from Window.Ratchet.push.

http://goratchet.com/components/#push

于 2015-03-03T13:20:13.773 回答
0

Ratchet.js 将您的网站变成一个单页应用程序。我认为您在表单字段中有输入,当单击按钮时会导致发布,这会导致意外的页面导航。如果是这种情况,解决方案是在 onclick 事件处理程序中返回 false

button onclick="tryLogin();return false;">Try Login</button>

例子

于 2015-03-04T05:11:00.380 回答