1

I have a list of variables available to me and I want to send it via $.ajax post. What format would I have to keep these in to use the function .serialize? I keep getting this error:

Object 'blank' has no method 'serialize'

I've tried to make them an array and I've tried jQuery.param(). I have a feeling this is simple but I can't seem to get it. Thanks!

var $data = jQuery.makeArray(attachmentId = attachmentID, action = 'rename', oldName = filename, newName, bucketName, oldFolderName, newFolderName, projectId = PID, businessId = BID);
var serializedData = $data.serializeArray();
//alert(theurl);

$.ajax({ type: "post", url: theurl, data: serializedData, dataType: 'json', success:  reCreateTree });  
6
  • 1
    Show your code, that way we can help you Commented May 30, 2012 at 14:13
  • 2
    .serialize is for form fields. Just use an object as value of the data: option. Commented May 30, 2012 at 14:14
  • 2
    What do you think jQuery.makeArray(attachmentId = attachmentID, ...) is doing? This is not how JavaScript works. .makeArray expects an object. Just have a look at the examples in the $.ajax documentation, you will find, e.g. data: {id : menuId},. I recommend to read the MDN JavaScript Guide to learn the basics of the language. Commented May 30, 2012 at 14:16
  • So I don't need .serialize? Would an object be sufficient? I have probably been over thinking the significance of .serialize Commented May 30, 2012 at 14:18
  • 1
    The .serialize documentation says: "Encode a set of form elements as a string for submission.". And the $.ajax documentation says for the data option: "Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below)." All the information you need is there ;) Commented May 30, 2012 at 14:20

2 Answers 2

3

.serialize is for form elements:

Encode a set of form elements as a string for submission.

The $.ajax documentation says for the data option:

Data to be sent to the server. It is converted to a query string, if not already a string. It's appended to the url for GET-requests. See processData option to prevent this automatic processing. Object must be Key/Value pairs. If value is an Array, jQuery serializes multiple values with same key based on the value of the traditional setting (described below).

So all you need to do is passing an object. For example:

$.ajax({ 
    type: "post", 
    url: theurl, 
    data: {                             // <-- just pass an object
          attachmentId: attachmentID,
          action: 'rename',
          // ...
    },
    dataType: 'json', 
    success:  reCreateTree 
});  
Sign up to request clarification or add additional context in comments.

Comments

3

It seems you're used to the PHP style of array's (associated arrays). In Javascript, objects are basically the same thing (though can be MUCH more complicated).

So if you are trying to create an array like this in php it would be

$array = array(
    "foo" => "bar",
    "bar" => "foo",
);

in Javascript using an object instead of an array it would be

var arr = {
    foo: "bar",
    bar: "foo"
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.