[英]Send ajax request
您好我有这样的代码
<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)
这个请求甚至没有去服务器,我在谷歌Chrome控制台看,但如果我运行sbt()
来自控制台请求的功能正常,如果我使用xmlhttp.open(...,...,false)
它工作正常,有人可以帮助我吗? 谢谢。
提交按钮将立即提交它所在的表单,浏览器将离开页面(以及从中发出Ajax请求的JavaScript执行环境)。
当您想要使用JavaScript时,取消表单提交的默认行为。
onclick="sbt(); return false;"
(我会考虑使用现代方法来进行事件绑定,而不是使用onclick
属性)。
这是您的整个页面代码吗?
你检查了jscript加载顺序吗?
请看看这个---> 小提琴 (这有3秒的响应延迟)
<h2>AJAX async demo</h2>
<button type="button" onclick="callAjax()">Request data</button>
<div id="testDiv"></div>
<script>
function callAjax() {
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("testDiv").innerHTML = xmlhttp.responseText;
}
};
xmlhttp.open("POST", "/echo/html/", true);
var params = "html=test&delay=3"
xmlhttp.send(params);
}
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.