0

I would like to send a list of data by ajax. So I push all data in a 2d array

But the ajax of jquery not accept the array data, the data must be object or query string

since the Object of javascript is no push function, I must use array to build the list of data. Is there any function in jquery or javascript, let me

var countLine=$("line").length;
var lines=$("line");
var lineArr=new Array();
var linesArr=new Array();
var x1, y1, x2, y2;
for(i=0; i<countLine; i++)
{
    lineArr['x1']=lines[i].getAttributeNS(null, "x1");
    lineArr['y1']=lines[i].getAttributeNS(null, "y1");
    lineArr['x2']=lines[i].getAttributeNS(null, "x2");
    lineArr['y2']=lines[i].getAttributeNS(null, "y2");
    linesArr.push(lineArr);
}
$.ajax({
    type: "POST",
    url: "test.php",
    data: linesArr,

    async: true,
    cache: false,

    success: function(data){
        $("#txt").text(data);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
        alert("fail");
    }
});
0

2 Answers 2

1

Change

data: linesArr

to

data: { linesArr: linesArr }

This will send an object with a single property, "linesArr", which contains the value of your array.


EDIT: you have a bigger problem. You are trying to store non-numeric properties in an array, as shown in this line:

lineArr['x1']=lines[i].getAttributeNS(null, "x1");

You are also reusing the same lineArr "array" each time through the loop. The following code would work much better:

var lines = $("line");
var linesArray = [];

for (var i = 0; i < lines.length; ++i) {
    var line = lines[i];

    linesArray.push({
        x1: line.getAttributeNS(null, "x1"),
        y1: line.getAttributeNS(null, "y1"),
        x2: line.getAttributeNS(null, "x2"),
        y2: line.getAttributeNS(null, "y2")
    });
}

Then you would access your data like $_POST["linesArr"][0]["x1"].

Sign up to request clarification or add additional context in comments.

4 Comments

I am not sure the data is sent correctly, I have tried different way to get the value, but I cannot get the value echo $_POST[0]['x1']; echo $_POST['x1'][0]; echo $_POST["linesArr"]['x1']; echo $_POST["linesArr"]['x1'][0]; echo $_POST["linesArr"][0]['x1']; echo $_POST[0]["linesArr"]['x1'];
thank you very much, now is success, and I have found that why I cannot access the data before, because the two word of "data: { linesArr: linesArr }" can not be same, so now the linesArr: linesArray is ok. But one thing I still not understand, why it is a big problem for storing non-numeric properties in an array and reusing the same lineArr "array"?
You can reuse the same name; that is not a problem. I changed my sample code to linesArray for clarity because I hate abbreviations, but you can feel free to change it back and things won't break.
As for why you shouldn't store non-numeric properties in an array... in JavaScript, as in most other languages, arrays are typical computer-science arrays, i.e. collections indexed by nonnegative numbers. PHP has a thing called an "array" that is really a map from any string to any object, which is confusing; in JavaScript such a map is called an object. So trying to put nonnumeric properties on a JavaScript array (or C# array, or Java array, or C++ array, or Pascal array...) just doesn't make sense, and will give nonsensical results and/or errors.
1

You need to convert the object to a JSON string using JSON.stringify().

http://json.org/

4 Comments

JSON.stringify(linesArr) to be exact, but this answer is good.
The JSON.stringify() is also accept Object, so I need convert the array to object first.
stringify() will accept arrays, objects and scalars. No conversion is necessary.
Please try these code, in the first alert box, it will display [], while in the second box, it will display {"x":3,"y":4}, so I said it is only accept object. 'code'( var arr=new Array(); var obj=new Object(); arr['x']=1; arr['y']=2; obj.x=3; obj.y=4; alert(JSON.stringify(arr)); alert(JSON.stringify(obj));)

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.