Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass a JSON array into the URL?

JavaScript/JQuery

var arr=[];

$('.element').each(function(i)
{
    arr.push({"id":i,"value":$(this).attr('data-value')});
});

$.get('/json.php?arr='+arr,function(result)
{
    alert(result);
});

PHP

<?php

$j = json_decode($_GET['arr'], true);

foreach($j as $k => $v)
{
    echo $v['id'].':'.$v['value'].'<br />';
}

?>

Problem

But the problem is that the URL looks like /json.php?arr=[object Object],[object Object] instead of /json.php?arr=[{"id":1,"value":"value 1"},{"id":2,"value":"value 2"}]. Do I need to convert the object to a string? But I don't want to use another library other than JQuery. Is this possible? :/

like image 661
isuckatcoding Avatar asked Dec 06 '25 13:12

isuckatcoding


1 Answers

Try JSON.stringify

$.get('/json.php?arr='+JSON.stringify(arr),function(result)
like image 187
Musa Avatar answered Dec 08 '25 03:12

Musa



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!