AnsweredAssumed Answered

Parsing JSon response of REST Login (POST)

Question asked by koopa on Jul 9, 2014
Latest reply on Jul 10, 2014 by koopa
I'm working with REST API to Login in Share. My Javascript look like this:
<javascript>
   $.ajax({
      type: "POST",
      url: alfrescoRoot + "/service/api/login",
      cache: false,
      contentType: "application/json; charset=utf-8",
      data: JSON.stringify(usr),
      success: function (json) {
         console.log("resp = " + JSON.stringify(json));
         resp = JSON.parse(JSON.stringify(json));
         $("#ajax-message").css("color", "green");
         $("#ajax-message").html("LOGIN OK!");
      },
      error: function (json) {
         console.log("resp = " + JSON.stringify(json));
         resp = JSON.parse(JSON.stringify(json));
         var message = "code: " + resp.responseJSON.status.code +
            "\nname: " + resp.responseJSON.status.name + "\ndescription: " +
            resp.responseJSON.status.description;
         console.log(message);
         $("#ajax-message").css("color", "red");
         $("#ajax-message").html(message);
      }
   });
</javascript>

This is the response:
<javascript>
{
   "readyState":4,
   "responseText":"{\n    \"status\" : \n  {\n    \"code\" : 403,\n    \"name\" : \"Forbidden\",\n    \"description\" : \"Server understood the request but refused to fulfill it.\"\n  },  \n  \n  \"message\" : \"06090078 Login failed\",  \n  \"exception\" : \"\",\n  \n  \"callstack\" : \n  [ \n  \t   \n  ],\n  \n  \"server\" : \"Community v4.2.0 (4480) schema 6.020\",\n  \"time\" : \"9-lug-2014 14.38.30\"\n}\n\n",
   "responseJSON":{
      "status":{
         "code":403,
         "name":"Forbidden",
         "description":"Server understood the request but refused to fulfill it."
      },
      "message":"06090078 Login failed",
      "exception":"",
      "callstack":[

      ],
      "server":"Community v4.2.0 (4480) schema 6.020",
      "time":"9-lug-2014 14.38.30"
   },
   "status":403,
   "statusText":"Forbidden"
}
</javascript>

in case of error (suppose 403) the server response includes plain/text and other things and to find json response I have to parse 'responseJSON' data inside the response. Isn't there a way to receive just json response?

Thanks

Outcomes