[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)
?>
~~~
- 基本概念
- 服務器
- PHP學習
- PHP根據數據生成頁面
- form表單提交數據到服務器
- form表單查詢信息詳情頁
- 列表渲染展示以及跳轉詳情
- PHP拆分寫法
- form表單提交
- get方式提交數據補充
- post方式提交數據
- post上傳文件
- 請求報文和響應報文基本概念
- XMLHTTPRequest對象的基本使用
- 回調函數&獲取返回的數據
- ajax發送get請求
- ajax驗證用戶是否存在邏輯
- ajax發送post請求
- 新浪云使用方法
- onload 和 onreadystatechange
- XML格式
- 服務器返回XML格式數據
- JSON格式
- 服務器返回JSON格式數據
- ajax工具函數封裝
- js模板引擎
- 跨域解決方案
- JSONP
- CORS解決跨域
- 下載文件功能