我想在Scala.js中使用sendBeacon
API並以ByteBuffer
的形式發送二進制數據,但我無法將其轉換爲BodyInit
。我使用的是boopickle,它生成一個包含我的實例的編碼數據的二進制文件ByteBuffer
。如何將ByteBuffer轉換爲Scala.js中的BodyInit?
如上所述,我試圖將ByteBuffer
轉換爲ArrayBuffer
並將其轉換爲BodyInit
。但是,在Firefox 53.0.3上運行此代碼時,我沒有收到運行時錯誤,而是有效負載僅包含[object ArrayBuffer]
,而不包含二進制數據本身。
下面的代碼:
import scala.scalajs.js.typedarray.TypedArrayBufferOps._
import boopickle.Default._
import org.scalajs.dom.experimental.BodyInit
import org.scalajs.dom.experimental.beacon._
case class Message(firstName: String, lastName: String)
val message = Message("John", "Doe")
val data = Pickle.intoBytes(message).arrayBuffer()
dom.window.navigator.sendBeacon("/api", data.asInstanceOf[BodyInit])
謝謝!那爲我做了。賞金在2小時內是你的。 –