I am sending a GET request to backend rest service, as follows:
def showAllEmployees = Callback {
org.scalajs.dom.ext.Ajax.get(url = "http://localhost:8081/fetchAllEmployees").onComplete {
case Success(xhr) => {
Callback.log(xhr.responseText)
}
case Failure(t) => println("An error has occurred: " + t.getMessage)
}
}
I want to extract the records from the response. I observe that code inside case Success is not even executed.
So what is idiomatic way of doing it in scalajs-react?
You are wrapping your ajax call into
Callback, it means in order to ex ecute it - you have to call.runNow()In case of
Successresponse you are doingCallback.log(xhr.responseText)which isCallbackthat needs to evaluated again, but as you are insideonCompletewhich returnsUnit- it is not possible to do. So, ether doCallback.log(xhr.responseText).runNow()or just useprintlnWorking example: