你好,我有这样的代码
<button onclick="sbt()" name="step1[save]" type="submit" class="btn-type5 next-btn-form pie" value="Далее">Send</button>
function sbt(){
var phone = document.getElementById('fld1').value;
var xmlhttp;
if (window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}}
xmlhttp.open("GET","host.com/send.php?phone="+phone+"&t="+Date.now(),true);
xmlhttp.send();
}
我想异步发送这个请求,但是当我使用 xmlhttp.open(...,...,true)
这个请求甚至不去服务器,我在google chrome 控制台,但是如果我从控制台请求运行 sbt()
函数就可以了,如果我使用 xmlhttp.open(...,...,false)
它工作正常,有人可以帮助我吗?谢谢。
最佳答案
提交按钮将立即提交它所在的表单,浏览器将离开页面(以及发出 Ajax 请求的 JavaScript 执行环境)。
当您想改用 JavaScript 时,取消表单提交的默认行为。
onclick="sbt(); return false;"
(我会考虑使用 modern approach to event binding 而不是 onclick
属性)。
关于javascript - 发送ajax请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24889905/