Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery ajax calls on Scala.js

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?

like image 283
alcarv Avatar asked Feb 27 '14 14:02

alcarv


1 Answers

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])
like image 186
sjrd Avatar answered Nov 10 '22 14:11

sjrd