Jump to content

How to send http request from UniGui


MOGSY

Recommended Posts

It is very usefull to be able to send to different apps and recieve from different apps. There are many situations that it is required to commuinicate with different servers i.e phone servers.

I think it is better if possible to send and receive from the server side.

 

Regards

Link to comment
Share on other sites

Hi

I have tried sending httprequest as shown in the following using UniHTMLFrame. If I use a gateway the message is sent correctly, however if it sent directly it only sends "Option" issue with Cross-Origin Resource Sharing (CORS).

How is possible to overcome this and not use a gateway?

Thanks

 

<script>
    var request = new XMLHttpRequest();

    var HTTPServerIP = "192.168.1.10";
    var HTTPServerPORT = "8765";
    var HTTPServerVersion = "V6";
    var HTTPServerURL =
      "http://" +
      HTTPServerIP +
      ":" +
      HTTPServerPORT +
      "/API2/CITYGUIDE/" +
      HTTPServerVersion +
      "/";
      var ContentType = "application/json";
    var versionID = "v5";

    request.open('POST', HTTPServerURL);
    request.setRequestHeader("Content-Type", ContentType);
    request.setRequestHeader("authorization", 'BASIC TWFyYW5keTpDaXR5R3VpZGU=');
    request.responseType = 'json'
    request.send(JSON.stringify({ "command":"Testing","Message":"This is test message"

       }))
   </script>
 

Link to comment
Share on other sites

If you are making an http request from the client (which you do when you use JS, as all JS runs in the browser client) then you can only communicate with the local computer through CORS, and the local webserver must be configured to accept such requests. If you want to communicate to the outside world, you have to do it from the server side, and that is straightforward using e.g. idHTTP and doing a simple http get request.
 

Example of JS call to local webserver using CORS

UniSession.AddJS('$.get("http://127.0.0.1:'+uniMainModule.corsPort+'?event='+inttostr(EventID)+'&op='+uniMainModule.operator+'", function( data ){' +
     ' ajaxRequest(TransactionForm.form, ["cashRegisterEvent"], { response : data });  '+
   ' }); ');


Apache set up for CORS in httpd.conf

<VirtualHost *:80>
    DocumentRoot "c:\program files (x86)\cashreg\apache22\htdocs"
    Header set Access-Control-Allow-Origin "*"
</VirtualHost>


IndyHTTPServer set up for CORS

 if sameText('127.0.0.1', ARequestInfo.RemoteIP) and (ARequestInfo.CommandType=hcGet) and (length(aRequestInfo.Params.text)>0) then begin
    AResponseInfo.CustomHeaders.AddValue('Access-Control-Allow-Origin', '*');
    EventIDStr:=aRequestInfo.Params[0];

 

Link to comment
Share on other sites

Thank you very much Ron

what are "uniMainModule.corsPort", "EventID" and "uniMainModule.operator" ?

UniSession.AddJS('$.get("http://127.0.0.1:'+uniMainModule.corsPort+'?event='+inttostr(EventID)+'&op='+uniMainModule.operator+'", function( data ){' +
     ' ajaxRequest(TransactionForm.form, ["cashRegisterEvent"], { response : data });  '+
   ' }); ');
Link to comment
Share on other sites

  • 1 year later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...