0

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.

Selcuk
  • 57,004
  • 12
  • 102
  • 110
Hassan Abbas
  • 1,166
  • 20
  • 47
  • What you have here `'"{\\"json\\":[{...}"'` is probably not what you want. You will need to remove the leading and trailing double-quotes from the Python string before feeding ti to json.loads() – nigel222 May 19 '16 at 11:47

1 Answers1

1

You are trying to re-encode the JSON string. You need to use json.loads() instead:

result = json.loads(body)
Selcuk
  • 57,004
  • 12
  • 102
  • 110
  • If i use that i end with something like this u'json': [{u'id': u'3880-HCI-user-human-computer-interaction' in all the list – Hassan Abbas May 19 '16 at 11:45
  • @HassanAbbas The returned `result` is a Python dictionary. You can find your expected list in `result["json"]`. Try printing it using `pprint(result["json"])` – Selcuk May 19 '16 at 11:46
  • The problem is that i dont want 'u' to be coming in every element. If i do it your way result[json] i do get a list but character 'u' in every element. – Hassan Abbas May 19 '16 at 11:50
  • 1
    @HassanAbbas The `u` denotes that it is a Unicode string. You can't get rid of it and it won't affect your use of the list. See [this question](http://stackoverflow.com/questions/2464959/whats-the-u-prefix-in-a-python-string) for a detailed explanation. – Selcuk May 19 '16 at 11:52
  • 1
    Thanks for the help. Now i have seen 'u' does not effect anything. – Hassan Abbas May 19 '16 at 12:00