Lazarus Backend Problem

Develop and Deploy Cross-Browser Web Apps .

Lazarus Backend Problem

Postby wahono77 » Wed May 29, 2013 9:46 am

I use Lazarus/FreePascal to create CGI to manage MySQL Database :

Project file : acosyscgi.lpr
delphi code
program acosyscgi;

{$mode objfpc}{$H+}
{.$define cgidebug}

uses
fpcgi, customer_handler;

begin
Application.ModuleVariable := 'm';
Application.Initialize;
Application.Run;
end.

customer_handle.pas :
delphi code
unit customer_handler;

{$mode objfpc}{$H+}

interface

uses
SysUtils, Classes, httpdefs, fpHTTP, fpWeb, sqldbwebdata, fpwebdata,
fpjsonrpc, webjsonrpc, mysql55conn, mysql51conn, sqldb, db, lazutf8;

type

{ Tcustomers }

Tcustomers = class(TFPWebModule)
wmysql: TMySQL55Connection;
wquery: TSQLQuery;
wcount: TSQLQuery;
wtrans: TSQLTransaction;
procedure DataModuleCreate(Sender: TObject);
procedure DataModuleDestroy(Sender: TObject);
procedure readRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
procedure SQLDBWebDataProvider1GetDataset(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;

var
customers: Tcustomers;

implementation

{$R *.lfm}

uses inifiles,fpjson,jsonparser;

{ Tcustomers }

procedure Tcustomers.DataModuleCreate(Sender: TObject);
Var
FN : String;
Ini : TMemIniFile;
begin
FN:='acosyscgi.ini';
If FileExists(FN) then
begin
Ini:=TMemIniFile.Create(FN);
try
With wmysql do
begin
HostName:=Ini.ReadString('Database','Host',HostName);
DatabaseName:=Ini.ReadString('Database','Database',DatabaseName);
UserName:=Ini.ReadString('Database','UserName',UserName);
Password:=Ini.ReadString('Database','Password',Password);
Port:=Ini.ReadInteger('Database','Port',Port);
end;
finally
Ini.Free;
end;
end;
wmysql.Connected:=True;
end;

procedure Tcustomers.DataModuleDestroy(Sender: TObject);
begin
wmysql.Connected:= false;
end;

procedure Tcustomers.readRequest(Sender: TObject; ARequest: TRequest;
AResponse: TResponse; var Handled: Boolean);
var
str1, str2, str3, afilter, acontent, arow, apage, acount: string;
i, j: integer;
begin
afilter:= '';
for i:=0 to ARequest.QueryFields.Count-1 do begin
str2:= ARequest.QueryFields.Strings[i];
j:= UTF8Pos('=', str2, 1);
str3:= UTF8Copy(str2, 1, j-1);
case str3 of
'province', 'name', 'address':
begin
if afilter<>'' then
afilter:= afilter + ' and ' + str3 + '=''' + UTF8Copy(str2, j+1, UTF8Length(str2)-j) + ''''
else
afilter:= ' ' + str3 + '=''' + UTF8Copy(str2, j+1, UTF8Length(str2)-j) + '''';
end;
'page':
begin
apage:= UTF8Copy(str2, j+1, UTF8Length(str2)-j);
end;
'count':
begin
acount:= UTF8Copy(str2, j+1, UTF8Length(str2)-j);
end;
end;
end;
if afilter<>'' then begin
wquery.SQL.Text:= 'select * from customers where '+ afilter + ' order by name';
end else begin
wquery.SQL.Text:= 'select * from customers order by name';
end;
wquery.SQL.Text:= wquery.SQL.Text + ' limit ' + inttostr((strtoint(apage)-1) * strtoint(acount)) + ', ' + acount;
wquery.Active:= true;
wcount.SQL.Text:= 'select count(*) as customercount from customers where '+ afilter;
wcount.Active:= true;
acontent:= '[[' + inttostr(wcount.FieldByName('customercount').AsLongint) + '],';
if wquery.recordcount>0 then begin
acontent:= acontent + '[';
wquery.First;
while not wquery.EOF do begin
arow:= '["' + wquery.FieldByName('name').AsString + '", "' +
wquery.FieldByName('address').AsString + '"]';
acontent:= acontent + arow;
wquery.Next;
if not wquery.EOF then
acontent:= acontent + ','
else
acontent:= acontent + ']';
end;
end;
acontent:= acontent + ']';
AResponse.ContentType:= 'application/x-javascript';
AResponse.Content:= acontent;
Handled:= true;
wquery.Active:= false;
end;

procedure Tcustomers.SQLDBWebDataProvider1GetDataset(Sender: TObject);
begin

end;

initialization
RegisterHTTPModule('customers', Tcustomers);
end.


This CGI run normally in browser IE, FireFox, Safari and Chrome, but I got problem in CrossUI desktop. If I test it with CrossUI Service test, I got message "Error: SAjax return script doesn't match"

I call CGI with CrossUI like this :

javascript code
getlist : function (page){
if(!page)page=1;
var apagestr, apage;
apagestr = spacust.ctl_pager.getValue();
apage = apagestr.split(':');
xui.request("http://localhost/cgi-bin/acosyscgi/customers/read",
{
"province" : spacust.ctl_province.getUIValue(),
"page" : parseInt(apage[1]),
"count" : 20
},
function(rsp){
var rspobj = rsp;
if(rspobj){
// handle result
var pc = parseInt(rspobj[0][0])||1;
spacust.ctl_pager.setValue([1,page,Math.ceil(pc/20)].join(':'));
spacust.treegrid.setRows(rspobj[1]);
}else{
// handle exception
spacust.treegrid.setRows({});
}
}, function(rsp){
// handle exception
spacust.treegrid.setRows({});
}, null, {method:'get'});
},


How to fix it?

Thanks
wahono77
 
Posts: 90
Joined: Sat Apr 13, 2013 6:04 am

Re: Lazarus Backend Problem

Postby support » Wed May 29, 2013 12:33 pm

That's because your CrossUI's port is diffrent with your back-end's. It's a cross-domain call.

You should implement CrossUI's SAjax or IAjax back-end function.

I dont familiar with Delhpi, the followsing is php code:

php code
// wrap result data for xui.IAjax and xui.SAjax
if(_.isset($callbackV)){
// for xui.IAjax
if($callbackV=="window.name"){
$outputDataWrapped="<script type='text' id='json'>".$outputDataWrapped."</script><script type='text/javascript'>window.name=document.getElementById('json').innerHTML;</script>";
}
// for xui.SAjax
else{
$outputDataWrapped = $callbackV.'('.$outputDataWrapped.')';
}
}


Please go to http://www.crossui.com/Forum/crossui-php-backend-demo-t72.html for detail.
support
 
Posts: 340
Joined: Sat Apr 27, 2013 9:22 am

Re: Lazarus Backend Problem

Postby wahono77 » Wed May 29, 2013 1:06 pm

Thanks, my problem is fixed now. I add this lines :
pascal code
....
AResponse.ContentType:= 'application/x-javascript';
// for xui.IAjax
if (acallback='window.name') then
begin
acontent:= '<script type=''text'' id=''json''>' + acontent + '</script><script type=''text/javascript''>window.name=document.getElementById(''json'').innerHTML;</script>';
end else
begin
// for xui.SAjax
acontent := acallback + '(' + acontent + ')';
end;
AResponse.Content:= acontent;
Handled:= true;
wahono77
 
Posts: 90
Joined: Sat Apr 13, 2013 6:04 am


Return to CrossUI for Web Apps

Who is online

Users browsing this forum: No registered users and 2 guests

cron