Comments (11)
Yeap. :)
uses
BrookRequestHelper;
procedure TMyAction.Request(ARequest: TRequest; AResponse: TResponse);
begin
if ARequest.IsAjax then
Write('ajax')
else
Write('No ajax');
end;
from brookframework.
From the code (and my test) it seems to work only for POST request, while (to my understanding) ajax can be done for any request.
from brookframework.
No. The "Request" method is triggered independent of HTTP methods. If you try it:
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
BrookAction, BrookRequestHelper, HTTPDefs, SysUtils;
type
TMyAction = class(TBrookAction)
public
procedure Request(ARequest: TRequest; AResponse: TResponse); override;
end;
implementation
procedure TMyAction.Request(ARequest: TRequest; AResponse: TResponse);
begin
WriteLn(BoolToStr(ARequest.IsAjax, True));
end;
initialization
TMyAction.Register('*');
end.
<!DOCTYPE HTML>
<html lang="en-US">
<head>
<meta charset="UTF-8">
<title></title>
<script type="text/javascript" src="http://brookframework.org/assets/js/jquery.js"></script>
</head>
<body>
<button id="btn">Test</button>
<div id="data"></div>
<script type="text/javascript">
$(function() {
$("#btn").on("click", function() {
$.get("http://localhost/cgi-bin/cgi1", function(data) {
$("#data").html(data);
});
});
});
</script>
</body>
</html>
The result will:
[Test]
True
from brookframework.
Try using embedded http server with this code:
$.ajax({
type: "post",
data: { isajax: true },
url: url,
success: function(data) {
$('#content').html(data);
$('#content').show('slow');
},
error: function(jqXHR,text,err) {
$('#content').html('<div class="error">' + err + '</div>');
$('#content').show('slow');
}
});
ARequest.IsAjax is only true when type: "post" AND data: { isajax: true } are given. Looks like a multiple layer bug. The embedded HTTP server might not set HTTP_X_REQUESTED_WITH environment variable to XMLHttpRequest (in my test, this is empty all the time), ContentFields property (which isajax uses) should only contain POST values (AFAIK, feel free to confirm with Michael).
from brookframework.
Now I understood. :)
The "data" property in jQuery works only in POST method. But the HTTP_X_REQUESTED_WITH is a client variable and it is written in all HTTP methods.
The Brook implementation is very simple, and it is based on Slim framework code (https://github.com/codeguy/Slim/blob/master/Slim/Http/Request.php#L142 ):
function TBrookRequestHelper.IsAjax: Boolean;
begin
Result := (ContentFields.Values['isajax'] <> ES) or
(GetEnvironmentVariable(BROOK_CLT_ENV_HTTP_X_REQUESTED_WITH) = 'XMLHttpRequest');
end;
It detects Ajax request when:
. Client POST the field isajax=true;
. Client request the variable HTTP_X_REQUESTED_WITH of client;
Seems a bug in FCL HTTP app.
from brookframework.
The Brook implementation can be changed to:
function TBrookRequestHelper.IsAjax: Boolean;
begin
Result := (ContentFields.Values['isajax'] <> ES) or
(QueryFields.Values['isajax'] <> ES) or
(GetEnvironmentVariable(BROOK_CLT_ENV_HTTP_X_REQUESTED_WITH) = 'XMLHttpRequest');
end;
What do you think?
from brookframework.
I think that should be fine, now could you also verify that the environment variable is not defined when using embedded http server? Just to be sure before I file a bugreport
from brookframework.
Yes. The "QueryFields.Values['isajax']" also detects if you send the "isajax" in GET vars, eg:
But FCL HTTP app should return the HTTP_X_REQUESTED_WITH variable.
from brookframework.
It worked for you buddy?
from brookframework.
Yep, GET and POST are OK now
Sent from my Android phone with mail.com Mail. Please excuse my brevity.
Silvio Clecio [email protected] wrote:
It worked for you buddy?
—
Reply to this email directly or view it on GitHub.
from brookframework.
Thank you very much!
from brookframework.
Related Issues (20)
- Why plugins projects on github to brookframework is closing? HOT 4
- Telegram plugin for brookframework HOT 15
- i18n HOT 4
- Version 4 ? HOT 14
- libbrook.pas in Source HOT 3
- tardigrade demos throwing errors in libbrook HOT 4
- Server requirements? HOT 4
- dopf & LastInsertID HOT 4
- httprouter.lpr throwing error HOT 2
- How am I supposed to free worker thread in brook daemon? HOT 9
- Use legacy application with tardigrade HOT 19
- CPU Activity is high for tardigrade projects BROOK4 HOT 4
- More documentation HOT 1
- Can't get POST request data HOT 13
- Brookframework with sagui (legacy) HOT 11
- HTTP Client HOT 1
- Where did the captcha plugin for 4.0 go? HOT 1
- Some suggestion about JTemplate? HOT 3
- [IDEA] New repository HOT 1
- EBrookHTTPServer: Failed to send data in request for HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from brookframework.