简体   繁体   中英

Array of JSON objects successfully sent to php using jquery ajax but php reports array size=0

I've created array of json objects as

var jsnObjs = [{"key": 0, "data": 1}, {"key": 1, "data": 2}];

Output of console.log(jsnObjs) in chrome is as under

(2) [Object, Object] 0: Object key: 0 data: 1 proto : Object 1: Object key: 1 data: 2 proto : Object length: 2 proto : Array(0)

also the success:function (given below) shows data transmitted successfully. But the on var_dump(jsnos) in the php file I get Array (size=0).

function doAjaxRequest(jsnObjs) {       
  jq.ajax({
    url: "viewajaxdata.php",
    type: "post",
    data: {jsnos : jsnObjs},
    contentType: "application/x-www-form-urlencoded",   
    success: function (response) {
      alert("Ajax Transmitted successfully");
    },
    error: function (request, status, error) {
      alert("Error: data tranmission failed !\n" + error);
    }       
  });    
}   

The viewajaxdata.php file is :

<?php
ini_set("display_errors", "On");
var_dump($_POST);   
$ajaxadata = json_decode(stripslashes($_POST['jsnos']), true);
$n =  count($ajaxdata);
echo $n;
?>

Could anybody please throw some light as to where I'm going wrong.

You can use below code it will works

<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>

viewajaxdata.php

<?php
$n =  count($_POST['jsnos']);
echo $n;
?>

Friends, finally I could solve the issue. I made following changes in javascript data: {"jsnos" : JSON.stringify(jsnObjs)}, removed 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);
}
});
}

Then in my viewajaxdata.php I wrote the following

<!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>

The alert(response); show the following (the html markups are omitted here) K=0 data=1

Now, to solve my problem ahead, I shall write the success function as

success: function() {
alert("Ajax Transmitted successfully");
}

Then I shall code the php file to dynamically generate html form for editing the data values. Hope I shall be able to complete the problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM