-1

i have to parsing json data get from $post method

$.post( "vehicle_route_qry.php",function(data) {  

    siteshistory = $.parseJSON(data);
    siteshistory.shift();
    draw_route(siteshistory);
    $("#dev_det_spin").hide();

});

my php file

$myJSON = '{ "name":"John", "age":31, "city":"New York" }';
echo json_encode($myJSON);

i am getting json properly in my response but getting this issue

siteshistory.shift is not a function

i have loaded following js files

<script src="js/function.js"></script>
<script src="js/jquery.min.js"></script>
<script src="https://developers.google.com/maps/documentation/javascript/examples/markerclusterer/markerclusterer.js">
</script>
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDaL0ieAkLhzy1rDoLifajeowdXPwTvzmI"></script>
<!-- <script type="text/javascript" src="js/date.js"></script> -->
<script type="text/javascript" src="js/loader.js"></script>
<script type="text/javascript" src="js/header_function.js"></script>

but shift function for $.parseJSON(data).shift() is not working i am not able to find any reference regarding shift function

suggestion would be helpful

dhi_m
  • 1,235
  • 12
  • 21
Neeraj Verma
  • 2,174
  • 6
  • 30
  • 51

3 Answers3

0

Check this:

https://developer.mozilla.org/tr/docs/Web/JavaScript/Reference/Global_Objects/Array/shift

Your json should be an array, should not be an object to execute shift().

You can refer this question to translate object to array in JS :

https://stackoverflow.com/questions/6857468/converting-a-js-object-to-an-array-using-jquery

Pradeep
  • 9,667
  • 13
  • 27
  • 34
Ali insan Soyaslan
  • 836
  • 5
  • 14
  • 33
0

Use

`"delete siteshistory.name"`;

instead of

"siteshistory.shift()";
Pradeep
  • 9,667
  • 13
  • 27
  • 34
0

//what your trying to shift() is not an array try below code

 var siteshistory = [];

    siteshistoryObj = $.parseJSON('{ "name":"John", "age":31, "city":"New York" }');//add here your ajax response

   //converting to array
    $.each(siteshistoryObj, function (index, order) {
      siteshistory.push({index : index});
    });
    siteshistory.shift();

console.log(siteshistory);// check array values
K.B
  • 885
  • 5
  • 10