Victor Kimura
asked on
create Array and jQuery Object as Key/Value for .ajax()
Hi,
I'm trying to pass in the data option for the jquery .ajax() like:
This page http://api.jquery.com/jQuery.ajax/ states 'data' should be:
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).
I'm trying to figure out how to create this Array or Object as Key/Value pairs in the format above.
For instance, I was trying this:
I'm wondering also how to do this manually like:
myArray.push({
'testid':'testval',
'testname':'testname'
});
in the format:
{
nametest:'jonny',
producttest:'productval'
}
and then keep adding to this array or Object as Key/Value in order for the .ajax() to work properly and pass the GET or POST vars.
I'm trying to pass in the data option for the jquery .ajax() like:
$.ajax({
type: "POST",
url: "some.php",
data: {
nametest:'jonny',
producttest:'productval'
}
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
This page http://api.jquery.com/jQuery.ajax/ states 'data' should be:
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).
I'm trying to figure out how to create this Array or Object as Key/Value pairs in the format above.
For instance, I was trying this:
var myArray = [];
var myObj;
function testCall() {
// we are using the array created above
var userList = $('#userList');
userList.children('li').each(function () {
var userID = $(this).attr('id');
var userName = $(this).text();
myArray.push({
'id':userID,
'name':userName
});
});
return myArray;
}
<ul id="userList">
<li id="user1">Jim</li>
<li id="user2">Mike</li>
<li id="user3">Jake</li>
</ul>
I'm wondering also how to do this manually like:
myArray.push({
'testid':'testval',
'testname':'testname'
});
in the format:
{
nametest:'jonny',
producttest:'productval'
}
and then keep adding to this array or Object as Key/Value in order for the .ajax() to work properly and pass the GET or POST vars.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
No worries - glad to help.
ASKER