javascript - 尝试使用jquery发布内容时如何使用while或类似的东西来发送数据
问题描述
我有一些这样的代码:
<script>
for(i=0;i<=5;i++){name[i]: "name",}
</script>
我知道这是错误的,因为我想使用它,但$.post
我不知道该怎么做
我试过这个并且这个工作很好,但我没有在这段代码中添加一段时间,因为我不知道如何做到这一点,我会告诉你我想要的地方:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
$.post("demo_test_post.asp",
{
name: "Donald Duck",
city: "Duckburg",
// for(i=0;i<=5;i++){name[i]: "name",} // it's my custom data and its not working
},
function(data,status){
alert("Data: " + data + "\nStatus: " + status);
});
});
});
</script>
</head>
<body>
<button>Send an HTTP POST request to a page and get the result back</button>
</body>
</html>
解决方案
我找到了这样做的方法并且它正在工作:
<script>
var data=[];
for(i=0;i<=5;i++){data.push(i)}
$.post( "test.php", { 'data[]': data } );
</script>
在test.php
我将收到$_POST['data']
一个数组,而不是像这样:
<?php
for($i=0;$i<=count($_POST['data'])-1;$i++){
print_r($_POST[$i]);
}
?>
推荐阅读
- php - 在 Laravel Valet 上安装自定义证书(用于使用外部服务)
- python - 如何使用 cURL 将文件作为 GET 请求上传
- c# - 操作 C# 速记 Getter-Setter
- r - 如何找到 R 中均值的合并标准误?
- android - 如何使用来自不同 DAO 的函数和使用协程与 Room 进行数据库事务?
- plsql - 在 PL/SQL 中使用 LIKE
- php - PHP Curl 使用限制和偏移在分页上执行 While 循环
- machine-learning - 具有一变量梯度下降的线性回归
- flask-sqlalchemy - 无法在托管服务 pythonanywhere 上打开数据库文件
- jsf - Prime Faces - 带有多个选定列表的多选复选框