1

Hi every one I need to send an array as the data of a post request using $.ajax, this array is a key value array, e.g

fields['BrandName'] = "Fiat,BMW"
fields['year'] =  "2004,2005"

i tried this

$.ajax({
    type: "POST",
    url: url,
    dataType: 'json',
    data: {'adfields[]': fields},
    success: function(data){
        alert("passed");                
    },
    error: function(data, status){
        alert("error has occurred");
    }
});

but it doesn't work, I watch the request using wireshark, but nothing gets sent in the request, something else when I try to do fields.length .. i get zero although fields['BrandName'] is defined and has a value Any help ??

2
  • there is some ways to do it look on zulius.com/how-to/… Commented Nov 6, 2011 at 9:51
  • 1
    How do you create fields in the first place? It looks like you are confusing Arrays and Objects. Commented Nov 6, 2011 at 9:57

2 Answers 2

3

Further to alFReD's answer, two ways you can code your fields variable as an object such that the key/value properties will be passed in your Ajax call:

// declare empty object
var fields = {};
// then add keys and values with the syntax you already had
fields['BrandName'] = "Fiat,BMW";
fields['year'] =  "2004,2005";

// OR
// declare in one statement
var fields = { 'BrandName' : "Fiat,BMW",
               'year' : "2004,2005" };

Note that the curly braces {} only apply in the initial declaration: once your object has been created you use square brackets to access the properties. Or, dot notation:

fields.BrandName = "Fiat,BMW";

(Dot notation only works if the keyname follows the same rules as other JS identifiers, e.g., no spaces, not starting with a number, not a reserved word, etc.)

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

Comments

0

Well You are just adding properties to the array(not array members, not numeric), that in fact since an array is an object, it accepts the properties but in serialization only the numbered properties will be written. Try using a plain object, I think that should work.

JavaScript arrays are list-like objects that come with a several built-in methods to perform traversal and mutation operations. Neither the size of an array nor the types of its elements are fixed. Since an array's size can grow or shrink at any time, arrays are not guaranteed to be dense. In general, these are convenient characteristics, but if these are desirable things to have for your use case, you might consider using WebGL typed arrays.

Note that you shouldn't use an array as an associative array. You can use plain objects instead, although doing so comes with its own caveats. See the post Lightweight JavaScript dictionaries with arbitrary keys as an example.

From MDN

Comments

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.