Forum > Pas2JS
restbridgeclient doesn't do 'GET'
(1/1)
rca:
I am working with example restbridgeclient (pasjs demo) and restserver (Lazarus demo) from sqldbrest.
When using jsonclient (Lazarus demo) it works OK and the server shows:
--- Code: Bash [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---[Resource] (User: ?) Resource: metadata; Operation: GET[Connection] (User: ?) Using connection to Host: localhost; Database: expensetracker[Authentication] (User: me) Authenticated user: me[Result] (User: me) Resource: metadata; Operation: GET; Status: 200; Text: OK
However, when using restbridgeclient example (pasjs demo), it doesn't work and the server shows:
--- Code: Bash [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---[Result] (User: ?) Resource: metadata; Operation: OPTIONS; Status: 403; Text: FORBIDDEN
The difference is in the Operation, it should be 'GET', but it sends 'OPTIONS'.
In the unit sqldbrestdataset, there is the "constructor TServiceRequest.Create", if I comment the following line of code
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---FXHR.setRequestHeader('Authorization', 'Basic '+window.btoa(aUserName+':'+aPassword));it sends the 'GET' method, but obviously it would not be authenticated and still does not work.
Please can you help me solve it?
Thank you
rca:
After finding and reading some information:
https://stackoverflow.com/questions/63752007/axios-request-method-changes-to-options-instead-of-get
https://stackoverflow.com/questions/21783079/ajax-in-chrome-sending-options-instead-of-get-post-put-delete
https://stackoverflow.com/questions/29954037/why-is-an-options-request-sent-and-can-i-disable-it
In short, it not only happens with the 'GET' method, it will also happen with 'POST', 'PUT' or 'DELETE'.
"OPTIONS request is part of the so-called preflight request which is needed to figure out the CORS headers to know what needs/is allowed to be sent to the server with the actual request."
More details about "Cross-Origin Resource Sharing (CORS)"
https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS
"CORS preflight"
https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request
So the solution is on the server side (restserver Lazarus demo), within Dispatcher options, enable "rdoHandleCORS" (Handle CORS requests) and that's it!
Enjoy!
Navigation
[0] Message Index