为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用原生的XMLHttpRequest对象提交表单

如何使用原生的XMLHttpRequest对象提交表单

心有法竹 2019-03-14 09:10:55
如何使用原生的XMLHttpRequest对象提交表单
查看完整描述

3 回答

?
牧羊人nacy

TA贡献1862条经验 获得超7个赞



1

2

3

4

5

6

7

8

9

10

11

12

13

14

var xhr = new XMLHttpRequest();

            xhr.onreadystatechange = function(){

                if(xhr.state == 4) {

                    if((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {

                        console.log(xhr.responseText);

                    } else {

                        alert("HttpRequest was unsccessful: " + xhr.status);

                    }

                }

            }

            xhr.open("post", "form.php", true);

            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

            var form = document.getElementById("info");

            xhr.send(serialize(form));

 


查看完整回答
反对 回复 2019-03-18
?
慕森卡

TA贡献1806条经验 获得超8个赞

就和你普通的原生ajax一样啊。不过路径选择表单的action,提交方式选择表单的method对应的。然后用js将你的表单对应项的值都填写进入ajax的参数中。执行就好了。如果有文件域的话更麻烦点,好像要使用啥对象来着,你百度下,不太记得了,以前做过。

查看完整回答
反对 回复 2019-03-18
  • 3 回答
  • 0 关注
  • 476 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信