Reputation: 1429
I'm making a VCL Forms Application with a TIdHTTPServer on the main form and a CommandGet of the IdHTTPServer procedure:
procedure TForm6.IdHTTPServerPaymentsCommandGet(AContext: TIdContext;
ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
path: string;
content: TFileStream;
begin
AResponseInfo.CharSet := 'UTF-8';
path := GetCurrentDir.Remove(GetCurrentDir.Length - 11) + 'index.html'; // Length('Win32\Debug') = 11
if pos('profile&userName=', ARequestInfo.UnparsedParams) > 0 then
begin
content := TFileStream.Create(path, fmOpenReadWrite);
try
AResponseInfo.ContentStream := content;
finally
content.Free;
end;
end;
end;
index.html contains:
<html>
<head>
<title>Profile</title>
<script type="text/javascript" src="scripts/script.js"></script>
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="pragma" content="no-cache">
</head>
<body onload="sampleAlert()"><!-- sample function from script.js -->
<!-- Page content -->
</body>
</html>
The problem is when I open index.html file in browser (without starting the application) the page works fine but when I start the VCL application I have no access to script.js (when test with IE9: 'The value of the property 'sampleAlert' is null or undefined, not a Function object'). Any idea how can to solve it.
Upvotes: 1
Views: 124
Reputation: 36654
Your code always returns the index.html file, independent of what document the client requests. So if the client received the index.html and tries to render it, the client will detect that there is a reference to a resource scripts/script.js, and send a HTTP GET request for it to your server. But the server ignores the name of the requested resource (which is given in the RequestInfo parameter), reads the index.html file again and returns its content instead of the JavaScript file. The client expected a valid JavaScript document, and receives a HTML document instead.
To fix it, check the ARequestInfo.Document
property, and return the content of the requested resource.
Upvotes: 3