admin管理员组文章数量:1333710
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
1 Answer
Reset to default 6Your 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.
本文标签: javascriptAssign an object to 39data39 property in 39ajax39 method of jqueryStack Overflow
版权声明:本文标题:javascript - Assign an object to 'data' property in '$.ajax' method of jquery - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742355371a2459300.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论