0

The goal

Send [{"ID": 1, "Name": "XBOX"}, {"ID": 2, "Name": "Playstation 3"}] via $.ajax() using jQuery.

The problem

I have this:

[...]

var object = $.parseJSON(data);

$.ajax({
    type: "POST",
    url: "laboratory.php",
    data: object,
    success: function(response) {
        console.log(response);
    }
});

And, in laboratory.php:

<?php print_r($_REQUEST); ?>

And finally, the return via console is:

Array
(
    [undefined] => 
)

This is what the data's variable means:

[{"ID": 1, "Name": "XBOX"}, {"ID": 2, "Name": "Playstation 3"}]

And this is what object means (by Chrome's console):

[Object, Object]

Can someone give me an idea?

3
  • you are POSTING data, have you looked in the $_POST structure? since you're passing an array of object o jquery, jquery will attempt to turn that into a string of form params. Commented Oct 10, 2013 at 19:36
  • Are you saying me to debug with print_r($_POST);? If yes, then I already did and the return is the same. Commented Oct 10, 2013 at 19:37
  • What exactly do you want to send to the server? json, or post params Commented Oct 10, 2013 at 19:38

3 Answers 3

3

have you tried using JSON.stringify:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

$.ajax({
    type: "POST",
    contentType: 'application/json',
    url: "laboratory.php",
    data: JSON.stringify(object), // using stringify
    success: function(response) {
       console.log(response);
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

It works! But it's kinda weird having to stringify the JSON object while the docs say that the data parameter can be a plain object... Perhaps a jQuery version issue?
Glad you got it working! :) Maybe something is buggy about the jQuery $.parseJSON() method, maybe it isn't passing a plain javascript object?
1

Here's the problem. passing your array of objects to $.param() (which is what jQuery does internally with data: that isn't a string) results in "undefined=undefined&undefined=undefined" which is exactly what you are getting back from php. Your array simply is in a format that jQuery can't understand. Since what you actually want to send is json, don't use $.parseJSON because it'll turn your json into an array and what you really want is just a string.

//var object = $.parseJSON(data);

$.ajax({
    type: "POST",
    url: "laboratory.php",
    data: data,
    success: function(response) {
        console.log(response);
    }
});

11 Comments

My $_REQUEST is now empty, Kevin.
odd... it should be a string... Though, you can force it to be in $_POST by doing data: {json: data} you'll see it at $_POST["json"] are you sure data is what you think it is?
I saw that, but if you console.log(data), does it give you the json string you are expecting?
This is the string: [{"ID": 1, "Name": "XBOX}, {"ID": 2, "Name": "Playstation 3"}]
is that a copy paste error? the "XBOX},? it's in your question that way too. Really shouldn't affect the outcome though, since so far we haven't done anything other than treat it as a string.
|
0

For multiple data, getJSON is better in my opinion:

$.getJSON('laboratory.php', function(json) {
            $.each(json, function(key, val) {
            //Getting the value of id
            val["ID"];
            //Getting the value of name
            val["NAME"];
             //DO whatever u want.
            });
});

2 Comments

The "PHP code" is a simple multidimensional array with utf8_encode(json_encode($array)). And this is the data.
I reallized after posting, sorry about that. I had the same problem so I decided to try getJSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.