Search code examples
javascriptjsonajaxresponse

how to pass value to ajax response and receive in success function


i have array which i want to pass in ajax response

$val = [10 , 20 , 30];

here is i want to pass $val somtehing like this but i dont know the method

return response()->json(['success'=> true, 'message' => 'Success' , 'val' ]);
 

this $val i dont know how i can pass and receive it in console here i want to receive

success: function (data) {

             if(data.success == true){
                 
                     toastr.success(data.message);
                     console.log(data.val);
             } 

can someone help me? to receive value in console?


Solution

  • You are not sending the val property in your response. So just like you have success and message in your json response, include a val property too and pass the $val variable.