i've got code takes form fields , converts them json string suitable passing rest api (the marathon rest api specifically). problem json.stringify double quotes form field values, numeric, causes ajax call fail. here's string code producing:
{"id":"basic-1","cpus":"0","mem":"32","instances":"1","cmd":"while [ true ] ; echo 'hello marathon' ; sleep 5 ; done"}
the mem, cpus, , instances values should not double quoted because they're numeric.
data: json.stringify($('form').mytojson());
mytojson function serializes form fields. that's working fine. problem json.stringify putting quotes around keys , values, including numeric values, causing ajax call fail.
the json.stringify function takes 2 arguments. first argument replacer function, can function or array. consider replacer function:
function replacer(key, value) { if (typeof value === "string") { return undefined; } return value; } json.stringify($('form').mytojson(), replacer);
i want modify replacer function not quote numeric values. i'm wondering if have use html5 form attributes differentiate between string , numeric form fields? consider form field:
<input id="cpus" type="number" name="cpus" value="0.1" />
how can use fact field has attribute type="number" in replacer function? want modify replacer function not quote values when form field of type number.
in order turn stringifyed string data, use json.parse(stringifyedstring);
simply removing quotes string won't convert "5"
number
, example. it'll make string character 5
.
Comments
Post a Comment