2

This is what I know on the subject:

We have an ajax function to which we can pass an object with properties like this:

    var ajaxRequest = {};

    ajaxRequest['type'] = 'POST';
    ajaxRequest['async'] = false;
    ajaxRequest['datatype'] = 'json';
    ajaxRequest['url'] = '/Query/getMydata';

    $.ajax(ajaxRequest);

one of these properties is the data parameter which is made of key / value pairs:

    ajaxRequest['data'] = {color: 'red' , name: 'Steve' }

I tried to do something like this:

    var oData = [];
    oData['color'] = 'yellow';
    oData['name'] = 'Fred';

    ajaxRequest['data'] = oData;

but it does not work.

So my question is: there is an object that I can assign to the 'data' parameter, or I am forced to build the string with concatenation?

EDIT==============

Maybe I did not explain, I know that the method can be created by code like this:

     var ajaxRequest = {
      type: 'POST',
      async: false
      ....
      };

but I'm using objects and properties because I need to make the method 'generic', then I will add the 'if' like this:

    function ajaxReq(data){
         var ajaxRequest = {};

         if( !data.isEmpty()){
             ajaxRequest['data'] = data;
         }

           ajaxRequest['type'] = 'POST';
           ajaxRequest['async'] = false;
           ajaxRequest['datatype'] = 'json';
           ajaxRequest['url'] = '/Query/getMydata';

           ...

           $.ajax(ajaxRequest);
    }
2
  • 1
    Don´t make it a string; ajaxRequest['data'] = {color: 'red' , name: 'Steve' }; Commented Feb 1, 2013 at 16:03
  • I had wrong writing, sorry Commented Feb 1, 2013 at 16:23

1 Answer 1

6

Your complex method is entirely unnecessary. You should just be using object literals:

var ajaxRequest = {
    type: 'POST',
    async: false
    datatype: 'json',
    url: '/Query/getMydata'
};

$.ajax(ajaxRequest);

You can also nest them, so you could have this:

var ajaxRequest = {
    type: 'POST',
    async: false
    datatype: 'json',
    url: '/Query/getMydata',
    data: {
        color: 'yellow',
        name: 'Fred'
    }
};

jQuery will convert this into a query string for you, so you don't need to worry about that.


An additional clarification... The reason that oData = [] is causing problems is that [] creates an array. In Javascript, arrays are a special kind of object. Only properties with numeric keys are considered members of the array (e.g. oData[1]). If you'd used an object literal ({}) as you did with ajaxRequest, it would have worked fine.

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

3 Comments

I've edited the question to make it clear what I wanted to do. And I knew what you wrote in the first part of your answer. I had not noticed that I created an array instead of an object Litteral, and the second part of your answer solved my poblem! Thank you!
@benVG Fair enough, but it would make sense to construct as many elements as possible within the object literal, i.e. those that are constant. Note also that you can do ajaxRequest.url = '/Query/getMydata', for instance, to save characters and neaten code.
I did not know this,it will certainly help me!

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.