#1 2011-10-27 11:42:07

RangerX
Member
Registered: 2011-10-27
Posts: 6

JavaScript client

Hello! Somebody tried to make the web-client for use with mORMot?
There is client-side code (just in .html file on local hard drive) for submit button on login form (uses jquery):

$(function() {  
  $("#signin_submit").click(function() { 
  var dataString = 'username=admin&password=synopse';

$.ajax({
  type: "GET",
  cache: false,
  dataType: "jsonp", 
  url: "http://localhost:888/r/auth",
  data: dataString,

  success: function(data, textStatus, jqXHR) {
    alert('success: ' + data + textStatus);
  },

  error: function(jqXHR, textStatus, errorThrown) {
    alert("status: " + textStatus);
  }  
});  
return false;
  });  
});

There is simply server-side code:

var
  fModel: TSQLModel;
  fDB: TSQLRestServerDB;
  fServer: TSQLite3HttpServer;

procedure TfmMainServer.FormCreate(Sender: TObject);
begin
  fModel := TSQLModel.Create([TSQLAuthGroup, TSQLAuthUser],'r');
  fDB := TSQLRestServerDB.Create(fModel, ChangeFileExt(paramstr(0),'.db3'), True);
  fDB.NoAJAXJSON := False;
  fDB.CreateMissingTables(0);
  fServer := TSQLite3HttpServer.Create('888',[fDB]);
  fServer.OnlyJSONRequests := False;
end;

When i clicked on "Submit", browser firing up a "error" event with statusText = "parseerror". This is because mORMot HTTP server responses with content-type: application/json, but actually server return plain text string with "server nonce".
If change dataType from "jsonp" to "script", success event firing, but data parameter is "undefined" and i can't get "server nonce" sad
My question is: how to get "server nonce" with cross-domain AJAX (JSONP)? Or small example with mORMot RESTful authentication using browser (javascript) smile

P.S. Sorry for my English

Offline

#2 2011-10-27 11:55:32

ab
Administrator
From: France
Registered: 2010-06-21
Posts: 14,659
Website

Re: JavaScript client

There is no such sample code yet.

The framework is AJAX ready (from its RESTful and JSON-based design), but I've not used it this way yet.
I'm waiting for some frameworks to be released - like http://op4js.optimalesystemer.no or http://www.elevatesoft.com/products?category=ewb - in order to use the same Delphi code on both side (this is a good idea for an ORM).

You are using the mORMot authentication scheme, by setting "True" parameter in TSQLRestServerDB.Create.
The authentication process is quite secured, but also complex: you can not just use it as you implemented in your javascript code.
See the documentation about the process itself.

So my advice is not to use authentication at first (let parameter to FALSE).
You are certainly getting errors because you are attempting to get some content without any previous valid authentication.
That is the reason why the Server rejects your request.

Offline

Board footer

Powered by FluxBB