cancel
Showing results for 
Search instead for 
Did you mean: 

Parsing JSon response of REST Login (POST)

koopa
Confirmed Champ
Confirmed Champ
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
3 REPLIES 3

kaynezhang
World-Class Innovator
World-Class Innovator
Your prolem seems to be cross origin problem.
Since you are working with share,why not use Alfresco.util.Ajax class,it support csrf ,you don't need to parse "responseJSON"

I think it's not a cross-origin problem because when the call succeed I get:
<javascript>{"data":{"ticket":"TICKET_97b533b53720b4efc2888941e0d07f3d61587750"}}</javascript>
I'll try with Alfresco.util.Ajax.request… could you tell me wich js files I have to include?

Thank you

kaynezhang
World-Class Innovator
World-Class Innovator
alfresco.js