-6

I have an array like this

Array
(
    [0] => 131

    [1] => 103

    [2] => 21

    [3] => 94

    [4] => 107

    [5] => 130

    [6] => 92

    [7] => 128

    [8] => 115

    [9] => 81
)

How to pass this array in ajax call

Javascript Code is:

$(document).ready(function(){
 var recentchatfriendids = []; 
//actual array
 $.ajax({ 
    url: '/friends/message/getmessagestatus';
    data: {
        recentchatfriendids : recentchatfriendids
    },
    success: function(response){
    },
  }); 
});
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140

4 Answers4

0

convert it into json and try it. hope that works for you.

Tristup
  • 3,603
  • 1
  • 14
  • 26
  • 3
    this should be a comment as OP has provided little information on the issue at hand – Rotimi Mar 31 '17 at 10:39
  • @PhpDev Thanks for your information. I answered based on what I understand, – Tristup Mar 31 '17 at 10:42
  • The point is this isn't an answer. If you *show* the OP *how* to convert it to JSON then it may be. – Rory McCrossan Mar 31 '17 at 10:44
  • As the question is not specifying the nature of the array whether it is in PHP or Javascript, I answered in a generalised way that user can understand what exactly to do with the array. Hoping that the user knows how to convert it into JSON in PHP and JS both. – Tristup Mar 31 '17 at 10:48
0
die(json_encode($arr));

json_encode converts PHP array to JSON object. die() method sends the response back to client and terminates the PHP script

0

Basically you can:

  1. Convert the php array into a JSON string, using json_encode function, eg

    $array_variable = array([0] => 131....); echo json_encode($array_variable,true);

  2. Then use $.ajax({... to receive the echoed string and manipulate it.

Hope this helps...

Waweru Kamau
  • 1,429
  • 14
  • 20
-1

In Php :
----------------
$array = array(131,103,21,94,107,130,92,128,115,81);

echo json_encode($array,1);

In JqueryCode : 
----------------------
$.ajax({
  url:'http://yourUrl.com/service', // Your Service Url
  data:{data1:data1,data2:data2}  // If You want to send data form ajax call
  type:"POST" // POST OR GET You Want
  dataType : "json" // It is important for when you pass json data to ajax
  success : function(response){
     // You Can Get Json Response
  }    

});