Javascript Scala.js jQuery ajax调用示例
我尝试使用scala.js,但有些东西我不懂。我将感谢你的帮助Javascript Scala.js jQuery ajax调用示例,javascript,jquery,ajax,this,scala.js,Javascript,Jquery,Ajax,This,Scala.js,我尝试使用scala.js,但有些东西我不懂。我将感谢你的帮助 import org.scalajs.dom.raw.HTMLElement import org.scalajs.jquery.{jQuery => JQ, JQueryAjaxSettings, JQueryXHR} def tableEvents() = JQ("td > a").click { () => JQ.ajax(js.Dynamic.literal( `type` = "
import org.scalajs.dom.raw.HTMLElement
import org.scalajs.jquery.{jQuery => JQ, JQueryAjaxSettings, JQueryXHR}
def tableEvents() =
JQ("td > a").click { () =>
JQ.ajax(js.Dynamic.literal(
`type` = "GET",
url = "/update",
data = "id="+({ (x: HTMLElement) => JQ(x).parent().prev().html() }: js.ThisFunction),
// Not working. How to get "this" element here?
// Can I use something like Map or Seq to send of many parameters?
dataType = "json",
success = { (data: js.Any, textStatus: String, jqXHR: JQueryXHR) =>
// how to convert data: js.Any to Json ?
}
).asInstanceOf[JQueryAjaxSettings])
}
我试图在这里重复已经在工作的JavaScript代码:
function tableEvents() {
$("td > a").click(function() {
$.ajax( {
type: "GET",
url: "/update",
data: {id: $(this).parent().prev().html()},
dataType: "json",
success: function(data) {
$("#id").html(data.id);
$("#name").val(data.name);
$("#score").val(data.score);
}
})
})
}
这里需要将
此
参数传递给指定的lambda。单击
。因此,您必须将该lambda转换为一个js.ThisFunction
:
JQ("td > a").click({ (thiz: HTMLElement) =>
// Now You can use thiz
}: js.ThisFunction)
我得到了json,所以:val json=json.parse(json.stringify(data))。没有更简单的办法吗?我不知道你现在在说什么/想做什么。无论如何,这可能值得一个单独的问题。如果您将data:js.Any更改为data:js.Dynamic,我相信您将不必执行JSON.parse(JSON.stringify(data)),因为它已经被声明为动态的