使用jquery ajax成功地将JSON对象数组发送到php,但php报告数组大小为0
我已经创建了json对象数组,如下所示使用jquery ajax成功地将JSON对象数组发送到php,但php报告数组大小为0,php,jquery,arrays,json,ajax,Php,Jquery,Arrays,Json,Ajax,我已经创建了json对象数组,如下所示 var jsnObjs = [{"key": 0, "data": 1}, {"key": 1, "data": 2}]; chrome中console.log(jsnObjs)的输出如下 (2) [对象,对象] 0:对象键:0数据:1proto:对象 1:对象键:1数据:2proto:对象 长度:2 proto:数组(0) 另外,success:函数(如下所示)显示成功传输的数据。但是php文件中的on var_dump(jsnos)是我得到的数组(s
var jsnObjs = [{"key": 0, "data": 1}, {"key": 1, "data": 2}];
chrome中console.log(jsnObjs)的输出如下
(2) [对象,对象]
0:对象键:0数据:1proto:对象
1:对象键:1数据:2proto:对象
长度:2
proto:数组(0)
另外,success:函数(如下所示)显示成功传输的数据。但是php文件中的on var_dump(jsnos)是我得到的数组(size=0)
viewajaxdata.php文件是:
<?php
ini_set("display_errors", "On");
var_dump($_POST);
$ajaxadata = json_decode(stripslashes($_POST['jsnos']), true);
$n = count($ajaxdata);
echo $n;
?>
谁能告诉我哪里出了问题。你可以使用下面的代码,它会起作用的
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var jsnObjs = [{"key":0, "data":1}, {"key":1, "data":2}];
function doAjaxRequest(jsnObjs) {
$.ajax({
url: "viewajaxdata.php",
type: "post",
data: {jsnos : jsnObjs},
contentType: 'application/x-www-form-urlencoded',
success: function(response) {
alert(response);
alert("Ajax Transmitted successfully");
},
error: function(request, status, error) {
alert("Error: data tranmission failed !\n" + error);
}
});
}
doAjaxRequest(jsnObjs);
</script>
var jsnObjs=[{“键”:0,“数据”:1},{“键”:1,“数据”:2}];
函数doAjaxRequest(jsnObjs){
$.ajax({
url:“viewajaxdata.php”,
类型:“post”,
数据:{jsnos:jsnObjs},
contentType:'application/x-www-form-urlencoded',
成功:功能(响应){
警报(响应);
警报(“Ajax传输成功”);
},
错误:功能(请求、状态、错误){
警报(“错误:数据传输失败!\n”+错误);
}
});
}
doAjaxRequest(jsnObjs);
viewajaxdata.php
<?php
$n = count($_POST['jsnos']);
echo $n;
?>
您可以使用下面的代码,它将起作用
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
var jsnObjs = [{"key":0, "data":1}, {"key":1, "data":2}];
function doAjaxRequest(jsnObjs) {
$.ajax({
url: "viewajaxdata.php",
type: "post",
data: {jsnos : jsnObjs},
contentType: 'application/x-www-form-urlencoded',
success: function(response) {
alert(response);
alert("Ajax Transmitted successfully");
},
error: function(request, status, error) {
alert("Error: data tranmission failed !\n" + error);
}
});
}
doAjaxRequest(jsnObjs);
</script>
var jsnObjs=[{“键”:0,“数据”:1},{“键”:1,“数据”:2}];
函数doAjaxRequest(jsnObjs){
$.ajax({
url:“viewajaxdata.php”,
类型:“post”,
数据:{jsnos:jsnObjs},
contentType:'application/x-www-form-urlencoded',
成功:功能(响应){
警报(响应);
警报(“Ajax传输成功”);
},
错误:功能(请求、状态、错误){
警报(“错误:数据传输失败!\n”+错误);
}
});
}
doAjaxRequest(jsnObjs);
viewajaxdata.php
<?php
$n = count($_POST['jsnos']);
echo $n;
?>
朋友们,我终于可以解决这个问题了。我在javascript中做了以下更改 数据:{“jsnos”:JSON.stringify(jsnObjs)}, 已删除contentType:“application/x-www-form-urlencoded”
function doAjaxRequest(jsnObjs) {
jq.ajax({
url: "viewajaxdata.php",
type: "post",
data: {"jsnos" : JSON.stringify(jsnObjs)},
success: function(response) {
alert(response);
alert("Ajax Transmitted successfully");
},
error: function(request, status, error) {
alert("Error: data tranmission failed !\n" + error);
}
});
}
然后在我的viewajaxdata.php中,我写了以下内容
<!DOCTYPE html>
<html>
<head>
<title>Send json by ajax to php</title>
</head>
<body>
<?php
if (isset($_POST['jsnos'])) {
var_dump($_POST['jsnos']);
$objs = json_decode($_POST['jsnos']);
echo 'K=' . $objs[0]->key . ' data=' . $objs[0]->keysdata;
}
?>
</body>
</html>
然后,我将对php文件进行编码,以动态生成用于编辑数据值的html表单。希望我能完成这个问题。朋友们,我终于可以解决这个问题了。我在javascript中做了以下更改 数据:{“jsnos”:JSON.stringify(jsnObjs)}, 已删除contentType:“application/x-www-form-urlencoded”
function doAjaxRequest(jsnObjs) {
jq.ajax({
url: "viewajaxdata.php",
type: "post",
data: {"jsnos" : JSON.stringify(jsnObjs)},
success: function(response) {
alert(response);
alert("Ajax Transmitted successfully");
},
error: function(request, status, error) {
alert("Error: data tranmission failed !\n" + error);
}
});
}
然后在我的viewajaxdata.php中,我写了以下内容
<!DOCTYPE html>
<html>
<head>
<title>Send json by ajax to php</title>
</head>
<body>
<?php
if (isset($_POST['jsnos'])) {
var_dump($_POST['jsnos']);
$objs = json_decode($_POST['jsnos']);
echo 'K=' . $objs[0]->key . ' data=' . $objs[0]->keysdata;
}
?>
</body>
</html>
然后,我将对php文件进行编码,以动态生成用于编辑数据值的html表单。希望我能够完成这个问题。
contentType:'application/json'
data:json.stringify(jsnObjs)
在javascript中,尝试删除JSNOs周围的双引号,并将json对象也字符串化。contentType:'application/json'
在javascript中的数据:json.stringify(jsnObjs),请尝试删除json的双引号,并将json对象字符串化。谢谢。这个解决方案奏效了。正如我所说,我的源文件(包含javascript)上的一切都很好。我已经试过了,以前也找到了工作。但是当我在浏览器中访问viewajaxdata.php时,我仍然会得到相同的消息。我的目标是在php中接收数据并在那里进行处理(比如说编辑/删除操作),而不是返回到js文件。同时编写$ids=json\u decode(stripslashes($\u POST['jsnos')),true);在php中显示注意:未定义索引:jsnoshanks。这个解决方案奏效了。正如我所说,我的源文件(包含javascript)上的一切都很好。我已经试过了,以前也找到了工作。但是当我在浏览器中访问viewajaxdata.php时,我仍然会得到相同的消息。我的目标是在php中接收数据并在那里进行处理(比如说编辑/删除操作),而不是返回到js文件。同时编写$ids=json\u decode(stripslashes($\u POST['jsnos')),true);在php中显示注意事项:未定义索引:jsnos