vue表單數(shù)據(jù)交互提交演示教程
歡迎來到 vue-form 表單提交演示間, 你有更好的建議,請告知樓主額!
1. 客戶端 html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<!-- 開發(fā)環(huán)境版本,包含了有幫助的命令行警告 -->
<script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
<!-- 生產(chǎn)環(huán)境版本,優(yōu)化了尺寸和速度 -->
<!--<script src="https://cdn.jsdelivr.net/npm/vue"></script>-->
<!-- axios -->
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
</head>
<body>
<div id="app">
<h1>歡迎來到 vue-form 表單提交演示間, 你有更好的建議,請告知樓主額!</h1>
<table class="table">
<thead>
<tr>
<th>box</th>
<th>new</th>
<th>rank</th>
<th>desc</th>
<th>title</th>
</tr>
</thead>
<tbody>
<tr v-for="(v,i) in tabData">
<td>{{v.box}}</td>
<td>{{v.new}}</td>
<td>{{v.rank}}</td>
<td><input type="text" v-model="tabData[i]['desc']"></td>
<td>{{v.title}}</td>
</tr>
</tbody>
</table>
<p>
<button @click="submit" type="primary">提交</button>
</p>
</div>
<script type="application/javascript">
var app = new Vue({
el: '#app',
data: {
tabData: [
{
"box": 21650000,
"new": true,
"rank": 1,
"desc": 'desc1',
"title": "Geostorm"
},
{
"box": 13300000,
"new": true,
"rank": 2,
"desc": 'desc2',
"title": "Happy Death Day"
}
],
form: {
firstName: 'Fred',
lastName: 'Flintstone'
}
},
methods: {
submit: function () {
/**
* 多維數(shù)組對象降級為可供 axios 使用的form表單序列化數(shù)據(jù)
**/
function jsonData(arr) {
let json = "";
function fors(data, attr=false) {
data = JSON.parse(JSON.stringify(data));
for (let key in data) {
if(Array.isArray(data[key]) || Object.prototype.toString.apply(data[key]) ==='[object Object]'){
fors(data[key], true);
} else {
if(attr){
json = json + '&'+ key + '[]' +'=' + data[key];
}else {
json = json + '&'+ key +'=' + data[key];
}
}
}
}
fors(arr);
return json;
}
console.log(jsonData(this.form));
console.log('---------------');
console.log(jsonData(this.tabData));
console.log('---------------');
// 提交用戶數(shù)據(jù)
axios({
url: './index.php',
method: 'post',
data: jsonData(this.tabData),
/**
* 1. 如果后臺可接受 application/json 方式提交數(shù)據(jù) 則:
* * 不需要 transformRequest 函數(shù)處理請求數(shù)據(jù)
* * 不需要 jsonData 函數(shù)對數(shù)據(jù)進行降維處理
* 2. PHP 后臺 可通過以下方式解析 application/json 數(shù)據(jù)
* * $form = file_get_contents('php://input');
* * $form = json_decode($form);
*
* 3. 當然:**樓主強烈建議**使用 application/json 方式提交數(shù)據(jù)。
* 4. 如果大家覺得麻煩可以考慮使用 JQ了
*/
transformRequest: [function (data) {
// Do whatever you want to transform the data
if(Array.isArray(data) || Object.prototype.toString.apply(data) ==='[object Object]'){
let ret = '';
for (let it in data) {
ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
}
return ret
} else {
return data;
}
}],
headers: {
'Content-Type': 'application/x-www-form-urlencoded'
}
}).then(function (res) {
console.log('數(shù)據(jù)提交成功');
console.log(res.data);
})
}
}
});
</script>
<style type="text/css">
table{border-collapse: collapse;border: 1px solid red;}
th,td{border: 1px solid red;}
</style>
</body>
</html>
2. 服務(wù)端,以PHP為例
<?php
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers:x-requested-with,content-type');
/**
* 接收 x-www-form-urlencoded form表單提交數(shù)據(jù)
*/
echo json_encode($_REQUEST);
/**
* 1. $GLOBALS ["HTTP_RAW_POST_DATA"]; 需要配置服務(wù)器才可以使用
* 2. file_get_contents('php://input'); 無須配置即可使用
*
* 接收 application/json 提交數(shù)據(jù)
*/
$form = file_get_contents('php://input');
$form = json_decode($form);
print_r($form);
以上這篇vue表單數(shù)據(jù)交互提交演示教程就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
vue如何將導(dǎo)航欄、頂部欄設(shè)置為公共頁面
這篇文章主要介紹了vue如何將導(dǎo)航欄、頂部欄設(shè)置為公共頁面問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
vue-video-player實現(xiàn)實時視頻播放方式(監(jiān)控設(shè)備-rtmp流)
這篇文章主要介紹了vue-video-player實現(xiàn)實時視頻播放方式(監(jiān)控設(shè)備-rtmp流),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-08-08
vue & vue Cli 版本及對應(yīng)關(guān)系解讀
這篇文章主要介紹了vue & vue Cli 版本及對應(yīng)關(guān)系,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
Vue中使用vue-count-to(數(shù)字滾動插件)詳細教程
這篇文章主要給大家介紹了關(guān)于Vue中使用vue-count-to(數(shù)字滾動插件)的相關(guān)資料,最近需要開發(fā)一個數(shù)字滾動效果,在網(wǎng)上找到一個關(guān)于vue-countTo的插件,覺得這個插件還不錯,需要的朋友可以參考下2023-09-09
vue前端框架vueuse的useScroll函數(shù)使用源碼分析
這篇文章主要為大家介紹了vueuse的useScroll函數(shù)源碼分析詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-08-08
Monaco-editor 的 JSON Schema 配置及使用介紹
這篇文章主要為大家介紹了Monaco-editor 的 JSON Schema 配置及使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-10-10
vue使用jsencrypt實現(xiàn)rsa前端加密的操作代碼
這篇文章主要介紹了vue使用jsencrypt實現(xiàn)rsa前端加密,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-09-09

