admin管理员组

文章数量:1427288

I have a javascript object that looks somthing like this:

var obj = {

    "name": "username",
    "userid": "9999",

    "object1": {
        "subObject1": {
            "subArray1": [],
            "subArray2": []
        },
        "subObject2": {
            "subArray3": [],
            "subArray4": []
        }
    },
    "object2": {
        "subObject3": {
            "subArray5": [],
            "subArray6": []
        }
    },
    "array1": [],
    "array2": []
};

I have tried to use a jQuery ajax call like this:

$.ajax({

    url: "test.php",
    type: "POST",
    dataType: "text",
    processData: false,
    data: obj,
    success: function(data, status) {

        alert("Sucsess");        
    }
});

The problem is that PHP doesn't receive anything. The $_POST variable is empty. I'm not sure what I'm doing wrong.

Thanks

I have a javascript object that looks somthing like this:

var obj = {

    "name": "username",
    "userid": "9999",

    "object1": {
        "subObject1": {
            "subArray1": [],
            "subArray2": []
        },
        "subObject2": {
            "subArray3": [],
            "subArray4": []
        }
    },
    "object2": {
        "subObject3": {
            "subArray5": [],
            "subArray6": []
        }
    },
    "array1": [],
    "array2": []
};

I have tried to use a jQuery ajax call like this:

$.ajax({

    url: "test.php",
    type: "POST",
    dataType: "text",
    processData: false,
    data: obj,
    success: function(data, status) {

        alert("Sucsess");        
    }
});

The problem is that PHP doesn't receive anything. The $_POST variable is empty. I'm not sure what I'm doing wrong.

Thanks

Share Improve this question asked Feb 8, 2010 at 22:16 KyprusKyprus 1154 silver badges9 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 4

First, include JSON2.js (Link at bottom of that page) on the page, then change your call to this:

$.post(
  "test.php", 
  data: JSON.stringify( obj ), 
  function(data, status) {
        alert("Sucsess");        
  });

Try out jQuery 1.4.1 the $.param function was pletely rewritten to support things like this.

Why not send it by using something like the json2 library to serialize the whole object as JSON, then send that via a single parameter? I don't know PHP but I would be stunned if there weren't dozens of alternative JSON parsers available.

I don't believe it is possible to send a data object like that.

If you wanted to do something like that you would have to serialize it before you send the data and then unserialize at the server. HTTP has it's limits.

本文标签: Sending a multilevel javascript object to the server with AJAX failsStack Overflow