Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

ArchStanton's avatar

How can I send a jquery object via post

I have a Jquery object

0: Object
    name: "Jim"
         age: "42"
 1: Object
    name: "Bill"
        age: "50"

I am trying to pass to my controller via

$.post( "playlist", 'items', function( data) {  console.log(data)});

In my controller I get a blank $_POST variable

if ( Request::ajax() ){
            print_r($_POST);
        }

Any ideas what I am doing wrong? There is no form I am just trying to send the object.

0 likes
10 replies
ArchStanton's avatar

Its the $.post - still trying to get to grips with the markdown formatting.

RachidLaasri's avatar

Try like that, without '

$.post( "playlist", items, function( data) {
    console.log(data)
});
RachidLaasri's avatar

Can you post your jQuery code to see where the "items" variables come from; because the code above worked for me.

sabuncuserhat's avatar
Level 3

Hi,

var students = [];
students = [
    { name : 'Jim', age: 18 },
    { name : 'John', age: 24 },
    { name : 'Serhat', age: 25 }
];
//console.log(students);
$.ajax({
    type: "POST",
    url: '/students',
    data: {"students" : JSON.stringify(students)},
    cache: false,
    success: function(result){
        alert(result);
    }
});
public function postStudents()
{
    return var_dump(json_decode(Input::get('students')));

}
nolros's avatar

Try the following:

$.ajax({
    type: "POST",
    url: '/students',
    data: {"students" : students },
    cache: false,
    success: function(result){
        alert(result);
    }
});
use Illuminate\Http\Request;

// assuming this is L5
public function postStudents(Request $request)
{
    return dd($request->only('student'));

}
2 likes
EgoJon's avatar

As a couple of people have referred to above, converting the object to a JSON string may be the way to go with json_encode and then all of the object contents should be available within the receiving scripts.

pmall's avatar

As a couple of people have referred to above, converting the object to a JSON string may be the way to go with json_encode and then all of the object contents should be available within the receiving scripts.

A model / collection is automagically converted into json when printed.

1 like
iLovetoCode's avatar

Most of the answers here have referred .ajax() method instead of .post() which you have asked.

You can use the jQuery Post method like this:

$.post("result.php",
    {
        name1: "Jim",
        age1: 42,
        name2: "Bill",
        age2: 50,
    })
    .done(function (result, status, xhr) {
        alert(result);
    })
    .fail(function (xhr, status, error) {
        alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)
    });

The .done() and .fail() are the jQuery Deferred events that will be called when the AJAX request is completed or failed. These are very useful when making AJAX calls.

Please or to participate in this conversation.