/
ExtAjax.scala
55 lines (40 loc) · 1.6 KB
/
ExtAjax.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package common
import upickle._
import org.scalajs.dom.XMLHttpRequest
import org.scalajs.dom.ext.Ajax
import shared._
class ExtAjax(ajax: Ajax.type) {
def postAsForm(url: String,
data: String = "",
timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = false) = {
val contentType = ("Content-Type" -> "application/x-www-form-urlencoded")
apply("POST", url, data, timeout, headers + contentType, withCredentials)
}
def postAsJson(url: String,
data: String = "",
timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = false) = {
val contentType = ("Content-Type" -> "application/json")
apply("POST", url, data, timeout, headers + contentType, withCredentials)
}
def apply(method: String,
url: String,
data: String = "",
timeout: Int = 0,
headers: Map[String, String] = Map.empty,
withCredentials: Boolean = false) = {
val ajaxReq = ("X-Requested-With" -> "XMLHttpRequest")
ajax.apply(method, url, data, timeout, headers + ajaxReq, withCredentials, "")
}
}
class ExtXMLHttpRequest(req: XMLHttpRequest) {
// def responseAs[T](implicit readWrite: ReadWriter[T]): T = read[T](req.responseText)
def ok = req.status == 200
}
object ExtAjax {
implicit def wrapperForAjax(ajax: Ajax.type) = new ExtAjax(ajax)
implicit def wrapperForXMLHttpRequest(req: XMLHttpRequest) = new ExtXMLHttpRequest(req)
}