我正在使用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的解决方案?
发布于 2015-03-03 21:20:14
您有没有试过在触发javascript事件的按钮上添加data-ignore="PUSH“属性?这看起来应该是可行的,您还可以查看PUSH.js并根据您的请求关闭这些选项。您可以从Window.Ratchet.push获取推送对象。
http://goratchet.com/components/#push
发布于 2015-03-04 13:11:00
Ratchet.js将您的站点转换为单页面应用程序。我认为您的输入是在一个表单字段中,当单击按钮时会导致post,这会导致意外的页面导航。如果是这种情况,解决方案是在onclick事件处理程序中返回false
button onclick="tryLogin();return false;">Try Login</button>example
https://stackoverflow.com/questions/24055945
复制相似问题