表单: RailsNotes
用户: dreamable
创建日期: 2021-02-04 01:59:51 UTC
更新日期: 2021-02-04 01:59:51 UTC
引用:(Table ID 3, Record ID 18)

标题 :
AJAX events
笔记 :

Failed to get data in JS, found this question

For AJAX returns, jQuery returns events likes

$(document).ready ->
  $("#new_message").on("ajax:success", (e, data, status, xhr) ->
    console.log(xhr);
  ).on "ajax:error", (e, xhr, status, error) ->
    console.log(xhr);

However, as shown in the Rails Guide, Rails-ujs returns only one parameter event with all other in detail attribute. So the code should be like

document.body.addEventListener("ajax:success", (event) => {
  const [data, status, xhr] = event.detail;
});
标签: