javascript - Invalid JSON Primitive when using a variable -
javascript - Invalid JSON Primitive when using a variable -
i have code makes ajax phone call mvc controller method , it'll work without hitch if this:
var obj = '{"titlename":"whatever"}'; $.ajax({ type: "post", url: "/titles/yo", contenttype: "application/json; charset=utf-8", datatype: 'json', data: obj, success: function (result) { $("#title_field").html(result.titlename); } }); but if this:
var stringed="whatever" var obj = '{"titlename":stringed}'; $.ajax({ type: "post", url: "/titles/yo", contenttype: "application/json; charset=utf-8", datatype: 'json', data: obj, success: function (result) { $("#title_field").html(result.titlename); } }); it craps out on me "invalid json primitive" error. maintain trying various single , double quote permutations, maintain giving me same error. how can insert string variable json object?
try this:
var stringed = "whatever"; var obj = '{"titlename": "' + stringed + '"}'; also may want take @ json2 library, can stringify info automatically.
javascript jquery json
Comments
Post a Comment