发送ajax请求 [英] Send ajax request

查看:82
本文介绍了发送ajax请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我有这样的代码

 < button onclick =sbt()name =step1 [save] type =submitclass =btn-type5 next-btn-form pievalue =Далее>发送< / 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();
}

我想发送此请求异步,但当我使用<$ c $时c> xmlhttp.open(...,...,true)这个请求甚至没有去服务器,我在谷歌Chrome控制台上看,但如果我运行 sbt()来自控制台请求的功能正常,如果我使用 xmlhttp.open(...,...,false)它工作正常,有人可以帮助我吗?谢谢。

解决方案

提交按钮会立即提交它所在的表格,浏览器将离开页面(和用于执行Ajax请求的JavaScript执行环境。)



当您想要使用JavaScript时,取消表单提交的默认行为。

  onclick =sbt(); return false; 

(我会看一下使用现代的事件绑定方法而不是 onclick 属性。) / p>

Hello i have such code

<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();
}

I want to send this request async, but when i use xmlhttp.open(...,...,true) this request doens't even go to server, i watch it in google chrome console, but if i run sbt() function from console request goes ok, and if i use xmlhttp.open(...,...,false) it works fine, can someone help me? Thank you.

解决方案

The submit button will immediately submit the form it is in, and the browser will leave the page (and the JavaScript execution environment from which the Ajax request is being made).

Cancel the default behaviour of the form submission when you want to use JavaScript instead.

onclick="sbt(); return false;"

(I'd look at using a modern approach to event binding instead of onclick attributes though).

这篇关于发送ajax请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆