事情是这样的:
有个table,里面有n行,n不确定,我点一下保存按钮,就要把这n行都传到后端,后端进行分析处理存进数据库,然后根据处理结果返回一个值给前端。
我想到的是,一行一个JSON,但这n个JSON,我不知道怎么传给后端。
求教。
------解决方案--------------------
json 数组
------解决方案--------------------
[{},{},{}]
这样一个{}代表一行,
------解决方案--------------------
json 数组传递。
------解决方案--------------------
JSONArray
------解决方案--------------------
http://localhost/html/1/ajaxj_query.php?q=[{%22name%22:%22zhang%22,%22age%22:36},{%22name%22:%22li%22,%22age%22:24},{%22name%22:%22wang%22,%22age%22:18}]
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<script type="text/javascript">
function onajax() {
var json = [ {
"name" : "zhang",
"age" : 36
}, {
"name" : "li",
"age" : 24
}, {
"name" : "wang",
"age" : 18
} ];
var $a = JSON.stringify(json);
//$a = "[{"name":"zhang","age":36},{"name":"li","age":24},{"name":"wang","age":18}]";
findX($a);
}
function findX(finder) {
var xmlHttp;
if (window.XMLHttpRequest)
xmlHttp = new XMLHttpRequest(); // code for IE7+, Firefox, Chrome, Opera, Safari
else
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); // code for IE6, IE5
if (xmlHttp == null) {
alert("Browser does not support HTTP Request");
return;
}
xmlHttp.onreadystatechange = function() {
if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
//var str = trim(xmlHttp.responseText);
var str = xmlHttp.responseText;
if (str == "") {
alert("好像有问题,什么都没查到!");
return;
}
//对str进行处理
applyQueryX(str);
}
}
var url = "ajaxj_query.php?q=" + finder + "&sid=" + Math.random();
xmlHttp.open("GET", url, true);
xmlHttp.send();
}
function applyQueryX(str) {
alert(str);
}
onajax();
</script>
</body>
</html>
<?php
header("Content-Type:text/html;charset=utf-8");
$q = $_GET["q"];
$t = json_decode($q);
//print_r($t);
echo 'success';