javascript - JSON.stringify() fails to work -
this question has answer here:
when trying use json.stringify()
method turn array of strings json object can pass through php script, stringify()
method fails return of significance.
this code input being passed through. not being tampered else.
function submititem() { try { var item = []; item.name = $('.itemtext').val(); item.type = $('.itemtype').val(); item.price = $('.itemprice').val(); item.color = $('.itemcolor').val(); item.desc = $('.itemdesc').val(); item.image = $('.itemimage').val(); item.gifttype = $('.itemgifttype').val(); item.avail = $('.itemavail').val(); item.giftable = $('.itemgiftable').val(); item.ranking = $('.itemranking').val(); item.basictrack = $('.itembasic').val(); item.vettrack = $('.itemvet').val(); item.month = $('.itemmonth').is(':checked'); item.hidden = $('.itemhidden').is(':checked'); item.id = $('.itemid').val(); //left in confirmation purposes var join = []; join[0] = 'test'; join[1] = 'tset'; console.log( json.stringify( join ) ); console.log(item); var jsonitem = json.stringify(item); console.log( jsonitem ); } catch (err) { console.log(err.message); } }
this produces following output in console:
as can see, log both json items returns []
rather json string of sort.
any potential reason occur appreciated.
thanks.
you're initializing "item" array. should initializing plain object ({}
):
var item = {};
when json serializer sees actual array, operates on numerically-indexed properties.
Comments
Post a Comment