[Lazarus] fpWeb, XHR2 and arraybuffer

Reimar Grabowski reimgrab at web.de
Wed Aug 1 19:21:31 CEST 2012


Hi,

I am trying to use fpWeb in a simple cgi app to send data to a webpage. I am using XMLHttpRequest2 on the JS side. My problem is that the response is null if I set the responseType to "arraybuffer". If I do not set a responseType at all I get a response.
My JS skills are not the best but I think the code is correct (at least that is how it is done in all the examples I studied).
But the cgi app seems to work correctly, too. If I point my browser to the cgi app it downloads the data to a file and this data is correct.
Most likely I am doing something stupid or missing something obvious but currently I don't know what to do or where to look to get this working.

The relevant code parts:
Server-side:

Procedure TFPWebModule1.DataModuleRequest(Sender: TObject; ARequest: TRequest;
  AResponse: TResponse; Var Handled: Boolean);

var
  StreamResponse: TMemoryStream;
  Data: array [0..7] of byte;

begin
  StreamResponse:=TMemoryStream.Create;
  try
    Data[0]:=0;
    Data[1]:=1;
    Data[2]:=2;
    Data[3]:=3;
    Data[4]:=4;
    Data[5]:=5;
    Data[6]:=6;
    Data[7]:=7;
    StreamResponse.Write(Data, SizeOf(Data));
    AResponse.ContentType:='application/octet-stream';
    AResponse.ContentLength:=StreamResponse.Size;
    AResponse.ContentStream:=StreamResponse;
    AResponse.Code:=200;
    AResponse.SendContent;
  finally
    StreamResponse.Free;
  end;
  AResponse.ContentStream:=nil;
  Handled:=True;
end;


Client-side:

function sendXHR() {
  var xhr = new XMLHttpRequest();
  xhr.open("GET", "http://devserver/cgi-bin/cgitest", true);
  xhr.responseType = "arraybuffer";
  xhr.onload = function() {
    var arrayBuffer = xhr.response;
  }
  xhr.send();
}

Any help is appreciated.
R.




More information about the Lazarus mailing list