Solved

create Array and jQuery Object as Key/Value for .ajax()

Posted on 2012-03-26
5
2,284 Views
Last Modified: 2012-03-27
Hi,

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

Open in new window


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

Open in new window


<ul id="userList">
    <li id="user1">Jim</li>
    <li id="user2">Mike</li>
    <li id="user3">Jake</li>
</ul>

Open in new window


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.
0
Comment
Question by:Victor Kimura
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 40

Assisted Solution

by:gurvinder372
gurvinder372 earned 334 total points
ID: 37770036
check this

var array = new Array();

$("#userList li").each(function(){
    array [ $(this).attr("id") ] = $(this).html();
});

return array;
0
 
LVL 29

Assisted Solution

by:Badotz
Badotz earned 166 total points
ID: 37770068
var array = new Array();

should be

var array = [];
0
 
LVL 40

Accepted Solution

by:
gurvinder372 earned 334 total points
ID: 37770107
check this code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
	<script src="jquery-1.4.2.min.js"></script>
	<script>
		$(document).ready(function(){
			var array = {};

			$("#userList li").each(function(){
				console.log( $(this).attr("id") );
				array [ $(this).attr("id") ] = $(this).html();
			});
			console.log( array );

			var x;
			while ( x in array )
			{
				console.log( x + " value is " + array[ x ] )
			}
		});
	</script>
</HEAD>

<BODY>
	<ul id="userList">
		<li id="user1">Jim</li>
		<li id="user2">Mike</li>
		<li id="user3">Jake</li>
	</ul>
</BODY>
</HTML>

Open in new window

0
 

Author Closing Comment

by:Victor Kimura
ID: 37770316
Thank you. That worked. =)
0
 
LVL 29

Expert Comment

by:Badotz
ID: 37770324
No worries - glad to help.
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do I post more than 1 item to php backend 24 60
Expanding/Collapsing Tree on SharePoint List 29 68
Jquery Calculation 3 43
How to strip HTML from this JS code using regex 29 46
In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question