Skip to content

Instantly share code, notes, and snippets.

@tyrcho
Forked from anonymous/Main.scala
Last active January 26, 2021 11:01
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyrcho/484e6ca68976aadb2cac63b55069acd8 to your computer and use it in GitHub Desktop.
Save tyrcho/484e6ca68976aadb2cac63b55069acd8 to your computer and use it in GitHub Desktop.
Scala.js Asynchronous REST call (Ajax) demo with JSON parsing - http://www.scala-js-fiddle.com/gist/484e6ca68976aadb2cac63b55069acd8
import util._
import dom.ext._
import scala.scalajs.concurrent.JSExecutionContext.Implicits.runNow
object ScalaJSExample extends js.JSApp{
def main(): Unit = {
val url =
"http://jsonplaceholder.typicode.com/posts/1"
val f=Ajax.get(url)
f.onComplete{
case Success(xhr) =>
val json=js.JSON.parse(xhr.responseText)
val title=json.title.toString
val body=json.body.toString
println(div(
h1(title),
p(body)).render)
case Failure(e) => println(e.toString)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment