会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132384个问题
Python 全系列/第八阶段:轻量级Web开发利器-Flask框架/Flask视图基础和URL 11207楼
Python 全系列/第四阶段:函数式编程和核心特性/函数式编程核心(高阶函数、闭包等) 11209楼
JAVA 全系列/第一阶段:JAVA 快速入门/IDEA的使用和第一个java项目 11211楼


<!DOCTYPE html>

<html lang="en">

<head>

    <meta charset="UTF-8">

    <title>03.ajax的post的参数传递</title>

</head>

<body>

    <h1>03.ajax的post的参数传递</h1>

    name:<input type="text" id="name"><br/>

    password:<input type="text" id='name'><br/>

    <input type="button" value="获取参数" onclick="submitForm()">

    <input type="button" value="获取参数-post" onclick="submitForm2()">

    <script>

        function submitForm(){

            var name = document.getElementById('name').value

            var pwd = document.getElementById('pwd').value

            url='http://httpbin.org/get'

            params='name='+name+'&pwd'+pwd

            var xhr=new XMLHttpRequest()

            xhr,open('post',url)

            xhr.setRequestHeader('Content-Type','application/x_wwww-form-urlencoded')

            xhr.send(params)

            xhr.onload = function(){

                console.log(xhr.responseText)

            }            

        }


            function submitForm2(){

                var name = document.getElementById('name').value

                var pwd = document.getElementById('pwd').value

                url='http://httpbin.org/post'

                params={'name':name,'pwd':pwd}

                var xhr=new XMLHttpRequest()

                xhr,open('post',url)

                xhr.setRequestHeader('Content-Type','application/json')

                xhr.send(params)

                xhr.onload = function(){

                    console.log(xhr.responseText)

                }            

            }

        </script>

</body>

</html>



报错原因:

03.Ajax的post的参数传递.html:31 Uncaught TypeError: Cannot read properties of null (reading 'value')

    at submitForm2 (03.Ajax的post的参数传递.html:31)

    at HTMLInputElement.onclick (03.Ajax的post的参数传递.html:12)

submitForm2 @ 03.Ajax的post的参数传递.html:31

onclick @ 03.Ajax的post的参数传递.html:12


Python 全系列/第七阶段:网页编程基础/Ajax 11215楼
JAVA 全系列/第四阶段:网页编程和设计/HTML5(旧) 11217楼
JAVA 全系列/第十四阶段:全文检索服务/Solr 11218楼
JAVA 全系列/(旧的隐藏)第八阶段:电商高级项目_架构/编码/电商ego-基于SOA架构_Dubbo使用_逆向工程_分页插件完成商品查询 11219楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637