企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
[TOC] >[success] # ajax发送post请求 <br/> ~~~ 'post'请求必须要将请求头设置为setRequestHeader('content-type', 'application/x-www-form-urlencoded') ~~~ <br/> >[success] ## 前端代码 <br/> ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Document</title> <script> window.onload = function () { // 点击事件 document.querySelector('input').onclick = function () { // 1. 创建对象 var xhr = new XMLHttpRequest(); // 2. 设置请求行(get请求数据写在url后面) xhr.open('post', 'postData.php'); // 3. 设置请求头(get请求可以省略,post不发送数据也可以省略) xhr.setRequestHeader('content-type', 'application/x-www-form-urlencoded') // 3.5 注册回调函数 xhr.onload = function () { console.log(xhr.responseText); } // 4. 请求主体发送(get请求为空,或者写null,post请求数据写在这里,如果没有数据,直接为空或者写null) // post请求发送数据写在send中 // key=value&key2=value2 xhr.send('name=西蓝花&friend=鸡蛋') } } </script> </head> <body> <h2>ajax发送post请求</h2> <input type="button" value="post请求"> </body> </html> ~~~ <br/> >[success] ## 后端代码 <br/> ~~~ <?php // 返回内容 echo '你过来了'; print_r($_POST) ?> ~~~