Delphi - DataSnap Authentication via AJAX

0

I am now starting to use authentication in DataSnap. I am trying to access methods via AJAX and is returning error 401 - Unauthorized. My request is as follows:

$.ajax({
        username: login,
        password: pass,
        async : false,
        cache: "false",
        dataType: 'json',
        type: 'get',
        url: url,
        success: function(data){...}

I did this by following this question, where the only difference is that I am not using jsonp .

When accessed by the browser, a box is displayed for user input and password, when I enter the credentials it allows me to continue.

Does anyone know how to solve?


EDIT 1

I have also tried the request using:

beforeSend: function (xhr) {
     xhr.setRequestHeader('Authorization', makeBaseAuth(login, pass));
    }

Instead of username and password .

    
asked by anonymous 22.03.2017 / 13:59

1 answer

0

Try as follows, in the ServerMethodsUnit1 class that is created together when you create the REST DataSnap add in the uses of class Web.HTTPApp . In the function that is requested when you pass the user and password, you must get the user and the password that comes from Header , so you should implement the following way to get this information.

var
  oWebModule: TWebModule;
  sBody: String;
begin
  oWebModule := GetDataSnapWebModule;
  sBody := oWebModule.Request.Content;
end;

So you can get the Header sent to DataSnap , as you're working with REST by default the LyfeCycle strong> Invocation , At each execution of a servermethod an instance of the class will be created and then destroyed. This way you can not work with a session, only if you create the project from the DataSnap Server, but it does not have the WebBroker to get the Header sent to DataSnap.

    
22.03.2017 / 18:23