Jump to content
DidierXT

TW3JSONP

Recommended Posts

Hi,

I'm trying to set up a "full stack" SMS C/S application as I'm definitely not fan of JS/Html.

So I'm testing a NodeJS server (TNJHTTPServer) with a client app (TW3JSONP). 

But client side, ws.OnDataReady is never called.

More, I have a "Uncaught ReferenceError: Invalid left-hand side in assignment  [line #1]"

Any help would be wellcome.

(Code below, and project attached)

Thanks

 

///////////////  SERVEUR    ///////////////

procedure TServer.Run;
var
  params: variant;
  repo : string;
begin
  var serveur := TNJHTTPServer.Create;
  serveur.Port := 8099;
  //
  serveur.OnAfterServerStarted :=
    procedure (sender: TObject)
    begin
      console.log("Serveur started, port:" + TNJHTTPServer(Sender).Port.toString);
    end;
  //
  serveur.OnRequest :=
    procedure (Sender: TObject;
               const Request: TNJHttpRequest;
               const Response: TNJHttpResponse)
    begin
      params := querystring.parse(url.parse(Request.Url).query, '&');
      repo := '';
      if AnsiUpperCase(params['service'])= 'ADD' then begin
        repo := '{"id"=' + IntToStr(TimeToMilliseconds(Time)) + '}';
      end
      else begin
        repo := '{"id"=0}';
      end;
      Response.end(repo, 'utf8');
    end;
  //
  serveur.Start;
end;

//////////////// CLIENT ///////////////////

procedure TForm1.btnEnvoyerClick(Sender: TObject);
var
  ws : TW3JSONP;
  data : string;
begin
  ws := TW3JSONP.Create;
  ws.OnDataReady := procedure (const Sender: TW3JSONP; const Success: boolean)
    begin
      moReponse.Text := moReponse.Text + #13 + Sender.Data;
      ws.Free;
    end;
  data := Format('{"quand":"%s", "note":"%s"}',
                 [DateToISO8601(StrToDateDef(edQuand.Text, Date)),
                  QuotedStr(moNote.Text)]
                );
  ws.Request('http://127.0.0.1:8099?'
             +'service=ADD'
             +'&data='+data
            );
end;

 

 

 

 

NodeApp2.zip

Share this post


Link to post
Share on other sites

Thanks for the zip file. It looks like your server is not answering in the right way. You're using JSONP to query on the client, so a simple "{"id"=nnn} -answer is not enough. You need to also answer with the callback, that JSONP adds. What it actually is, I can't say right away, but I'll continue to test.

 

Share this post


Link to post
Share on other sites

Ah, got it. There are problems in both the client and the server:

In the client you need to do the request like this:

  ws.Request('http://127.0.0.1:8099?'
             +'service=ADD'
             +'&data='+TString.EncodeUriComponent(data)
             +'&callback='
            );

The most important thing is to actually end the request with that '&callback='. If you look at the query in the server, something like this is sent:

/?service=ADD&data=%7B%22quand%22%3A%222017-11-03%22%2C%20%22note%22%3A%22%27Test%27%22%7D&callback=_TJSONP[16]

On the server you also have to pay attention on how you respond. So if the request was that one above, the reply needs to be:

_JSONP[16]("your answer here");

And if you use quotes in your reply, make sure to encode your answer before you send it back. For example, you're using "id"=nnn, which will cause problems to you otherwise.

Share this post


Link to post
Share on other sites

JSONP is a very obscure technology.

Essentially you need to generate Javascript that is then downloaded and injected into the DOM, and then a callback fires ... its a long otello like they say in starwars.

You should really use a WebSocket server, which just happens to be a normal http server with websocket on-top of it. Then you can use the much more powerful and stable TW3WebSocket client class instead.

Best of luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×