-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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