html代码
<html>
<head>
<title>Vue提交表单数据</title>
<script src="https://cdn.jsdelivr.net/npm/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<style type="text/css">
/*boder-collapse 为表格设置合并边框模型*/
table{border-collapse:collapse;border: 1px solid blue}
th,td{border:1px solid blue}
</style>
</head>
<body>
<div align="center" id="app">
<h1>欢迎来到vue-form表单提交演示间</h1>
<table>
<thead>
<tr>
<th>name</th>
<th>sex</th>
<th>age</th>
</tr>
</thead>
<tbody>
<tr v-for="(v,i) in tableData"> <!-- vue严格区分大小写-->
<td>{{v.name}}</td>
<td>{{tableData[i].sex}}</td>
<td><input type="text" v-model="v.age"></td>
</tr>
</tbody>
</table>
<p>
<button @click="submit">提交</button> <!-- “v-on:”为绑定html事件,简写为@ ;官方解释用于监听 DOM 事件-->
</p>
</div>
</body>
<script>
var app= new Vue({
el:'#app', //vue挂载的元素节点 可以是css选择符,HTML元素
data:{
tableData:[
{
"name":211,
"sex":"男",
"age":18
},{
"name":985,
"sex":"女",
"age":18
}],
form:{
first:"first record",
last:"last record"
}
},
methods: {
submit: function () {
function jsonData(arr) {
//Json.parse将字符串转换成json对象
//Json.stringify将json对象转换成json字符串
let json = "";
function fors(data,attr){
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];
}
}
}
return json;
}
fors(arr);
return json
}
// console.log(jsonData(this.tableData)); //&name[]=211&sex[]=男&age[]=18&name[]=985&sex[]=女&age[]=18
//提交用户数据
axios({
url:"http://www.easyadmin.com/index.php",
method:"post",
data:jsonData(this.tableData),
//transformRequest允许向服务器发送数据之前修改请求数据 可删除
transformRequest:[function (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 (response) {
console.log("数据提交成功");
console.log(response);
})
.catch(function (error) {
console.log(error);
});
}
}
});
</script>
</html>
php代码:
<?php
header("Access-Control-Allow-Origin:*");
$form = file_get_contents('php://input');
//$form = json_decode($form);
exit($form);
参考链接:https://blog.youkuaiyun.com/liguanjie8/article/details/84774737