Demo server Session API

amussa8 years ago

Hello,

Im trying to login on demo server through API.

API return success message:

$.post( "http://demo.traccar.org/api/session", { email: "***", password: "***" } );
responseText: "{"id":30770,"attributes":{},"name":"Akil Mussá","email":"***","readonly":false,"admin":false,"map":null,"distanceUnit":null,"speedUnit":null,"latitude":0.0,"longitude":0.0,"zoom":0,"twelveHourFormat":false,"password":null}"

...but account doesn't seems to be logger-in:

$.get( "http://demo.traccar.org/api/session/" );
responseText: "{}"
GET http://demo.traccar.org/api/session/ 404 (Not Found)

Is there any kind of restrictions on Demo server?

Thank you,
AM.

Anton Tananaev8 years ago

I guess that you are not using same web session, so Traccar session is not saved across requests.

amussa8 years ago

Hi Anton,

I am using this code.
What am I doing wrong?

Getting 404 at line
$.get("http://demo.traccar.org/api/session")

<html>
 <head>
  <script type="text/javascript" src="jquery-3.1.0.min.js" charset="UTF-8"></script>
 </head>
<body>
 <script type="text/javascript">
  $.post("http://demo.traccar.org/api/session", {email:"***", password:"***"})
   .done(function(data){
      $("body").append((JSON.stringify(data)));
      $("body").append("<br/>");
      $.get("http://demo.traccar.org/api/session").done(function(data){
        $("body").append((JSON.stringify(data)));
      });
   }
  );
 </script>
</body>
</html>
Anton Tananaev8 years ago

Because it's a cross-domain request, I think you need to use "withCredentials" parameter to keep sessions cookie.

amussa8 years ago

Anton, thank you.

It was a cross-domain problem.

This is the code that works:

<html>
<head>
  <script type="text/javascript" src="jquery-3.1.0.min.js" charset="UTF-8"></script>
</head>
<body>
<script type="text/javascript">
  $.ajaxSetup({
    crossDomain: true,
    xhrFields: {
        withCredentials: true
    }
  });
  $.post("http://demo.traccar.org/api/session", {email:"***", password:"***"})
   .done(function(data){
      $("body").append((JSON.stringify(data)));
      $("body").append("<br/>");
      $.get("http://demo.traccar.org/api/session").done(function(data){
        $("body").append((JSON.stringify(data)));
        window.location.replace("http://demo.traccar.org");
      });
   });
</script>
</body>
</html>