Reputation: 1316
My client code:
var http = new XMLHttpRequest();
var url = "http://localhost:8000/recData";
var params = JSON.stringify({json: screen_keywords});
console.log(params);
http.open("POST", url, true);
//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.onreadystatechange = function() {
//Call a function when the state changes.
if(http.readyState == 4 && http.status == 200) {
alert(http.responseText);
}
}
http.send(params);
My server:
def do_POST(self):
if self.path == "/recData":
content_length = int(self.headers.getheader('content-length'))
body = self.rfile.read(content_length)
result = json.dumps(body)
pprint(result)
The result is:
'"{\\"json\\":[{\\"id\\":\\"1453-HCI-user-human-computer-interaction\\",\\"x_cordinate\\":395.9217224121094,\\"y_cordinate\\":729.04296875,\\"keyword\\":\\"user-human-computer-interaction\\",\\"radius\\":\\"70.359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"4044-HCI-user-design\\",\\"x_cordinate\\":544.33837890625,\\"y_cordinate\\":773.467529296875,\\"keyword\\":\\"user-design\\",\\"radius\\":\\"50.359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"668-HCI-user-experience\\",\\"x_cordinate\\":328.0223083496094,\\"y_cordinate\\":460.09271240234375,\\"keyword\\":\\"user-experience\\",\\"radius\\":\\"73.703125\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"2714-HCI-user-enjoyment\\",\\"x_cordinate\\":562.9501342773438,\\"y_cordinate\\":268.96636962890625,\\"keyword\\":\\"user-enjoyment\\",\\"radius\\":\\"70.359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"2659-ip-ip-TCP\\",\\"x_cordinate\\":648.444091796875,\\"y_cordinate\\":813.05712890625,\\"keyword\\":\\"ip-TCP\\",\\"radius\\":\\"41\\",\\"obj_rel\\":true,\\"sub_rel\\":false},{\\"id\\":\\"1022-service-ecosystem-trust\\",\\"x_cordinate\\":743.697509765625,\\"y_cordinate\\":848.6085205078125,\\"keyword\\":\\"trust\\",\\"radius\\":\\"38.3359375\\",\\"obj_rel\\":false,\\"sub_rel\\":false},{\\"id\\":\\"1916-ip-ip-management\\",\\"x_cordinate\\":750.01123046875,\\"y_cordinate\\":681.5203247070312,\\"keyword\\":\\"ip-management\\",\\"radius\\":\\"85.0390625\\",\\"obj_rel\\":true,\\"sub_rel\\":true},{\\"id\\":\\"1769-service-ecosystem-service-design\\",\\"x_cordinate\\":653.7368774414062,\\"y_cordinate\\":903.925537109375,\\"keyword\\":\\"service-design\\",\\"radius\\":\\"53.0078125\\",\\"obj_rel\\":false,\\"sub_rel\\":true},{\\"id\\":\\"4516-HCI-user-fun\\",\\"x_cordinate\\":562.857177734375,\\"y_cordinate\\":884.592529296875,\\"keyword\\":\\"user-fun\\",\\"radius\\":\\"41\\",\\"obj_rel\\":false,\\"sub_rel\\":true},{\\"id\\":\\"2291-HCI-user-usability\\",\\"x_cordinate\\":436.70556640625,\\"y_cordinate\\":878.2095947265625,\\"keyword\\":\\"user-usability\\",\\"radius\\":\\"58.3515625\\",\\"obj_rel\\":false,\\"sub_rel\\":true}]}"'
I am sending the json to my server side, but i am unable to decode it on my server side. Since i want to use it as a list so i can use a for loop on python side by accessing each object. Please tell me how to fix this. My result variable look something like this currently as shown in the snippet. Help will be highly appreciated.
Upvotes: 0
Views: 873
Reputation: 59184
You are trying to re-encode the JSON string. You need to use json.loads()
instead:
result = json.loads(body)
Upvotes: 1