Question

On my app, I need to make some ajax calls and I am trying to use jQuery for it.

The way to do it would be by passing an object of type JQueryAjaxSettings to jQuery.ajax(url: String, settings: JQueryAjaxSettings). But I could not find a way to instantiate such an object.

I have tried to inherit the class with an object, a simple class, but none of these will work.

What's the way to go on this?

Was it helpful?

Solution

You will have to use js.Dynamic.literal to construct the settings arguments, and cast it to the appropriate type, like this:

import scala.scalajs.js
import org.scalajs.jquery._

jQuery.ajax(js.Dynamic.literal(
    url = "http://www.w3.org/People/Berners-Lee/card",
    success = { (data: js.Any, textStatus: js.String, jqXHR: JQueryXHR) =>
      console.log(s"data=$data,text=$textStatus,jqXHR=$jqXHR")
    },
    error = { (jqXHR: JQueryXHR, textStatus: js.String, errorThrow: js.String) =>
      console.log(s"jqXHR=$jqXHR,text=$textStatus,err=$errorThrow")
    },
    `type` = "GET"
).asInstanceOf[JQueryAjaxSettings])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top