jquery - JSON.parse: unexpected character when receiving assoc array from PHP json_encode -
update
so jquery.post() function receives valid json. know attribute "json" in function call should enough original array , .parsejson() not necessary. guess that's why syntax error when using function.
but somehow i still can't access array without .parsejson(). in fact, when use typeof(data) it's still string. can write console , it's still valid json.
how use array (originally associated php array encoded once via json_encode()) normal associated array in javascript?
original post
i've been getting syntax error days , it's driving me crazy. read lot of posts , know must super simple thing i'm missing. please help. in advance.
var institute = $.post("../libraries/load_content.php", { funct: "getinstituteaddress", ins_name: "institut für informatik", ins_city: "kiel" }, "json"); institute.done(function(data) { alert(data); var ins_data = $.parsejson(data); // <-- throws syntax error $("#street").empty().val(ins_data['ins_street']); $("#number").empty().val(ins_data['ins_number']); $("#postalcode").empty().val(ins_data['ins_postal_code']); }, "json"); institute.fail(function(data) { console.log("retrieving institute data failed. returned:"+data); });
the jquery.post() function calls php script, fetches required data db , returns associative array json_encode().
this works fine till point.
{"ins_name": "institut für informatik", "ins_street": "hrsl", "ins_number": "42", "ins_postal_code": "24118", "ins_city": "kiel"}
above alert returned string , jsonlint validator tells me it's valid json.
so copied string directly parsejson() call. worked fine. why not variable?
here's tried:
- i read missing single/double quotes. no luck here. added them in multiple variations acound string.
- i read parsejson function not required in such environment. dropped function call. syntax error of course disappears, can't read json normal array fields emptied.
- i tried replacing .post() function .ajax(), adding missing arguments... same effect.
- i changed datatype attributes html or text. again no luck.
Comments
Post a Comment