Giter Club home page Giter Club logo

Comments (11)

silvioprog avatar silvioprog commented on July 19, 2024

Yeap. :)

uses
  BrookRequestHelper;

procedure TMyAction.Request(ARequest: TRequest; AResponse: TResponse);
begin
  if ARequest.IsAjax then
    Write('ajax')
  else
    Write('No ajax');
end;

from brookframework.

leledumbo avatar leledumbo commented on July 19, 2024

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.

silvioprog avatar silvioprog commented on July 19, 2024

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.

leledumbo avatar leledumbo commented on July 19, 2024

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.

silvioprog avatar silvioprog commented on July 19, 2024

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.

silvioprog avatar silvioprog commented on July 19, 2024

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.

leledumbo avatar leledumbo commented on July 19, 2024

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.

silvioprog avatar silvioprog commented on July 19, 2024

Yes. The "QueryFields.Values['isajax']" also detects if you send the "isajax" in GET vars, eg:

http://localhost/foo?isajax

But FCL HTTP app should return the HTTP_X_REQUESTED_WITH variable.

from brookframework.

silvioprog avatar silvioprog commented on July 19, 2024

It worked for you buddy?

from brookframework.

leledumbo avatar leledumbo commented on July 19, 2024

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.

silvioprog avatar silvioprog commented on July 19, 2024

Thank you very much!

from brookframework.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.