所以每当我按下按钮时,我都会尝试将对象数组发送到js函数。
<button onclick="actualizarProcesos(<?php echo json_encode($p_array)?>);">X</button>
我已经确保我的json不会发送任何奇怪的字符,因为除了对象属性"name"是字符串之外,它大多是int。
该函数在另一个js文件中:
function actualizarProcesos(p_array){
var p = JSON.parse(p_array);
}
目前,我正在尝试发送,以确保函数正在接收数据,但它一直抛出错误Uncaught SyntaxError: Unexpected end of input
。因此,我一直在努力找出如何修复错误。
之后,我计划使用ajax将该数组发送到另一个php文件,类似于以下内容:
$.ajax({
type: "POST",
url: "post.php",
data: JSON.stringify(values),
success: function(data){
alert(data)
}
});
这是我试图发送的完整json
[{"name":"A","t_arrival":7,"t_est":25,"state":1,"pages":5,"mem_data":[[1,8,13,5,0,1],[0,0,0,0,0,0],[1,11,17,3,1,1],[1,12,16,4,0,1],[0,0,0,0,0,0]],"t_rem":25,"t_wait":0,"rem_quantum":0},{"name":"B","t_arrival":6,"t_est":13,"state":2,"pages":4,"mem_data":[[0,0,0,0,0,0],[1,9,16,5,0,1],[1,7,14,6,1,0],[0,0,0,0,0,0]],"t_rem":13,"t_wait":0,"rem_quantum":0},{"name":"C","t_arrival":8,"t_est":37,"state":3,"pages":3,"mem_data":[[1,9,12,2,0,0],[0,0,0,0,0,0],[1,13,21,7,0,1]],"t_rem":37,"t_wait":0,"rem_quantum":0}]
我已经确保我的json不会发送任何奇怪的字符,因为除了对象属性"name"是字符串之外,它大多是int。
JSON中的字符串用"
字符分隔。
<button onclick="actualizarProcesos(<?php echo json_encode($p_array)?>);">X</button>
您的HTML属性值由"
字符分隔。
数据中的"
将截断属性值。
您需要对数据进行编码(使用htmlspecialchars
(以嵌入HTML:
<?php echo htmlspecialchars(json_encode($p_array)); ?>
你可以用这种方式
<p id="test" style="display:none"><?php echo json_encode($p_array); ?></p>
<button onclick="actualizarProcesos(test)" id="">X</button>
<script src="https://code.jquery.com/jquery-1.9.1.min.js"></script>
<script>
function actualizarProcesos(test){
var p_array = $('#test').html();
var p = JSON.parse(p_array);
alert(p);
}
测试工作良好。希望这对你有所帮助。
<?php
$p_array = [
[
"name" => "A",
"t_arrival" => 7,
"t_est" => 25,
"state" => 1,
"pages" => 5,
"mem_data" => [
[
1,
8,
13,
5,
0,
1
],
[
0,
0,
0,
0,
0,
0
],
[
1,
11,
17,
3,
1,
1
],
[
1,
12,
16,
4,
0,
1
],
[
0,
0,
0,
0,
0,
0
]
],
"t_rem" => 25,
"t_wait" => 0,
"rem_quantum" => 0
],
[
"name" => "B",
"t_arrival" => 6,
"t_est" => 13,
"state" => 2,
"pages" => 4,
"mem_data" => [
[
0,
0,
0,
0,
0,
0
],
[
1,
9,
16,
5,
0,
1
],
[
1,
7,
14,
6,
1,
0
],
[
0,
0,
0,
0,
0,
0
]
],
"t_rem" => 13,
"t_wait" => 0,
"rem_quantum" => 0
],
[
"name" => "C",
"t_arrival" => 8,
"t_est" => 37,
"state" => 3,
"pages" => 3,
"mem_data" => [
[
1,
9,
12,
2,
0,
0
],
[
0,
0,
0,
0,
0,
0
],
[
1,
13,
21,
7,
0,
1
]
],
"t_rem" => 37,
"t_wait" => 0,
"rem_quantum" => 0
]
];
$p_array = json_encode($p_array);
?>
<button onclick='actualizarProcesos(<?php echo $p_array ?>)' >X</button>
<script>
function actualizarProcesos(arr){
console.log(arr);
}
</script>