ajax2-ajax的post请求
  TEZNKK3IfmPf 2023年11月14日 19 0

ajax2-ajax的post请求

 ajax2-ajax的post请求

 

// 引用expess框架
const express = require('express');
// 处理路径
const path = require('path');
 
const bodyParser = require('body-parser');
 
 
// 创建网站服务器
const app = express();
app.use(bodyParser.urlencoded());
app.get('/first', (req, res) => {
    res.send('hello geyao')
})
app.get('/responsdate', (req, res) => {
    res.send({ "name": "geyao" })
})
app.post('/post', (req, res) => {
    res.send(req.body);
})
app.get('/get', (req, res) => {
    res.send(req.query);
})
app.use(express.static(path.join(__dirname)));
// 监听端口
app.listen(3000);
console.log('网站服务器启动成功, 请访问localhost')

ajax3.html

<!DOCTYPE html>
<html lang="en">
 
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
 
<body>
    <p>
        <input type="text" id="username">
    </p>
    <p>
        <input type="text" id="age">
    </p>
    <p>
        <input type="button" value="提交" name="" id="btn">
    </p>
    <script>
        var btn = document.getElementById('btn');
        var username = document.getElementById('username');
        var password = document.getElementById('age');
        btn.onclick = function() {
            //1创建ajax对象
            var xhr = new XMLHttpRequest();
            //请求方式
            var nameValue = username.value;
            var ageValue = age.value;
 
            //
            var params = 'username=' + nameValue + '&&age=' + ageValue;
            xhr.open('post', 'http://localhost:3000/post');
            //设置参数请求格式类型
            xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xhr.send(params);
            //发送请求
 
            //获取数据
            xhr.onload = function() {
                console.log(xhr.responseText);
            }
        }
    </script>
</body>
 
</html>

运行结果

ajax2-ajax的post请求

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月14日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   19天前   31   0   0 ajaxxml
TEZNKK3IfmPf