Jump to content

[SOLVED] Turning "key:val" to {"key" : "val"} ?


MrFish

Recommended Posts

I have a client that loves the ajax/lightbox concept. Wants to use it on practically everything. I have several forms with many fields that I'm trying to make easier to submit with JQuery post. If you aren't familiar, JQuery post looks like this-

$.post(url, {"key" : "value",  "key2" : "value2"}, function(){});

And what I'm trying to do is loop through all of the inputs and build a string that I can submit easily like this. But I don't know how to take a string and turn it into this syntax. I don't really even understand this syntax since I only see it in objects and I've written objects a more old fashioned way I guess you can say. My code looks like this-

		window.QuickSubmit = function(formId, finish)		{			frm = document.getElementById(formId);			loc = frm.getAttribute("action");						eles = frm.elements;						str = "";						for(i = 0; i < eles.length; i++)			{				fld = eles[i];								tag = fld.tagName;								key = fld.getAttribute("name");				if(key == null)						continue;								if(tag == "SELECT")				{					value = fld.options[fld.selectedIndex].value;					if(value == null)						value = fld.options[fld.selectedIndex].text;				}				else				{					value = fld.getAttribute("value");				}								str += key + ":" + value + ",";			}						str = str.substr(0, str.length-1);						var d;						$.post(loc, eval("{" + str + "}"), d=finish);						return d;		}

I've tried different methods. My most resent was to use eval but I get an error when doing that-Uncaught SyntaxError: Unexpected token :

Link to comment
Share on other sites

I don't know why I didn't think of this before but I could just parse the whole string server side.I'm still a bit curious how I would do it like I originally planned.

Link to comment
Share on other sites

what does the string look like before you eval it? it might be worth validating it to make sure it's valid JSON first. I know on the occasions I've had to use eval before, I usually perform it in this convention:

eval('(' + someData + ')');

so given that you are trying to create an object, perhaps you should try something like

eval('({' + someData + '})');

anyway, I think the most important thing is to know what str looks like before you use it, and that it's actually valid JSON. also, this is the link to the JSON validator: http://jsonlint.com/

Link to comment
Share on other sites

I was able to get something from another forum on this that worked.

postData = {};...postData[key] = value;...$.post(url, postData, function(){});

Though- thanks for your reply, thescientist. I will remember to try that if I need to construct a JSON object like this again.

Link to comment
Share on other sites

right, well that will work because the simplest implementation of an object is just an associative array. probably easier and more practical than trying to eval a string.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...