最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Assign an object to 'data' property in '$.ajax' method of jquery - Stack Overflow

programmeradmin0浏览0评论

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);
    }

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);
    }
Share Improve this question edited Dec 24, 2015 at 12:48 Brian Tompsett - 汤莱恩 5,89372 gold badges61 silver badges133 bronze badges asked Feb 1, 2013 at 15:56 benVGbenVG 6133 gold badges14 silver badges26 bronze badges 2
  • 1 Don´t make it a string; ajaxRequest['data'] = {color: 'red' , name: 'Steve' }; – Stefan Commented Feb 1, 2013 at 16:03
  • I had wrong writing, sorry – benVG Commented Feb 1, 2013 at 16:23
Add a ment  | 

1 Answer 1

Reset to default 6

Your plex 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.

发布评论

评论列表(0)

  1. 暂无评论