python - Flask get_json does not return correct JSON -
i having problem decoding json flask.
from ajax send following request:
$.ajax ({ type: "post", url: 'http://localhost:5000/getsurveyresult/', datatype: 'json', contenttype: 'application/json', async: true, data: '{"1":"utf-16","2":"ja"}', success: function () { alert("thanks!"); } server side:
@app.route('/getsurveyresult/', methods=['post']) def get_survey_result(): request_json = request.get_json() print request_json return "ok" the print gives me weird output , cannot access fields using request_json[1]:
{u'1': u'utf-16', u'2': u'ja'}
the u in front of strings in printout signifies string unicode, it's standard python behaviour, see here : what 'u' symbol mean in front of string values?
as request_json variable, keys both strings , accessed request_json["1"] (note quotes around 1).
json allows keys strings design: https://www.w3schools.com/js/js_json_objects.asp
Comments
Post a Comment