【发布时间】:2019-02-10 12:06:41
【问题描述】:
我正在尝试向我的 rails 应用程序添加通知。基本上,我从 JSON 端点获取它们,然后尝试使用 CoffeescriptHere's the JSON array 获取它们并将它们附加到我的 html 中。问题出在 handleSuccess 方法中,我得到未捕获的引用错误,通知未定义
notifications.js.coffee
class Notifications
constructor: ->
@notifications = $("[data-behavior='notifications']")
@setup() if @notifications.length > 0
setup: ->
$.ajax(
url: "/notifications.json"
dataType: "JSON"
method: "GET"
success: @handleSuccess
)
handleSuccess: (data) =>
console.log(data)
items = $.map data, (notification) ->
"<a class='dropdown-item' href='#{notification.url}'>#
{notification.action} #{notification.notifiable.type}</a>"
jQuery ->
new Notifications
我从 localhost:3000/notifications.json 得到什么
[{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"}, {"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor" :"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen" ,"action":"liked","notifiable":{"type":"your list"},"url":"/lists/10#list_10"},{"actor":"emanuelcoen","action" :"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked" ,"notifiable":{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable" :{"type":"your list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type ":"你的名单"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"你的list"},"url":"/lists/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"}, “网址”:“/列表/17#list_17"},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17 "},{"actor":"emanuelcoen","action":"liked","notifiable":{"type":"your list"},"url":"/lists/17#list_17"}]
【问题讨论】:
-
感谢您的帮助!问题是,我在服务器日志中找不到错误。在服务器日志中,我只看到在每个页面加载时它都会构建 JSON,但之后我看不到错误在哪里。你能告诉我在哪里可以找到它吗?另外:我在 AJAX 调用中指定了 dataType JSON,为什么它不是数组?
-
http://yourserver/notifications.json返回什么? -
更新问题
标签: javascript ruby-on-rails coffeescript