post()提交数据

上传文件
form表单提交必须设置enctype=“multipart/form-data” 提交文件

ajax-post
1、需要发送数据时,请求头必须设置.setRequestHeader(‘Content-type’,‘application/x-www-form-urlencoded’)
2、发送的数据写在.send中
格式是 key=value&key2=value2

<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <h2>POST提交数据</h2>
    <input type="button" value="post请求">
</body>
<script>
    //点击事件
    document.querySelector('input').onclick = function(){
        //创建对象
        var xhr = new XMLHttpRequest();
        //请求行 post请求
        xhr.open('post','postData.php');
        //请求头 post请求是必写的
        xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded');
        //回调函数
        xhr.onload = function(){
            console.log(xhr.responseText);
        }
        //请求主体 返回数据 post请求发送数据写在send中
        //格式 字符串key=value&key2=value2
        xhr.send('name=西蓝花&friend=鸡蛋');
    }
</script>
</html>
<?php
    echo '数据 post 回来了';
    //接收数据
    print_r($_POST);
?>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值