JavaScript代码段在RubyonRails视图中不起作用

JavaScript代码段在RubyonRails视图中不起作用,javascript,ruby-on-rails,ruby,api,ruby-on-rails-4,Javascript,Ruby On Rails,Ruby,Api,Ruby On Rails 4,这是片段。它没有同步到我的api应用程序。我正在本地运行api应用程序和我正在跟踪的应用程序 <script> (function (){ // track a client-side event using the metrics analytics service var _bm_event = { name: "Topic Views", topic_name: "<%= @topic.name %>", created_at:

这是片段。它没有同步到我的api应用程序。我正在本地运行api应用程序和我正在跟踪的应用程序

<script> 
(function (){
    // track a client-side event using the metrics analytics service
  var _bm_event = {
    name: "Topic Views",
    topic_name: "<%= @topic.name %>",
    created_at: "<%= @topic.created_at %>"
  }

  var _bm_request = new XMLHttpRequest();
  _bm_request.open("POST", "http://localhost:3000/events", true);
  _bm_request.setRequestHeader('Content-Type', 'application/json');
  _bm_request.onreadystatechange = function() {
    // this function runs when the Ajax request changes state.
    // https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest
  };
  _bm_request.send(JSON.stringify(event));
}());</script>

如果您有任何见解或建议,我们将不胜感激。

您的API是否经过认证?Javascript日志返回的错误怎么办?我的API上没有任何身份验证。这是一个自定义的,我正在创建。另外,我刚开始使用Firebug yesternite,所以对itok不太熟悉,它没有出现在Firebug上的Javascript控制台中。问题如下_bm_request.sendJSON.stringifyevent应为_bm_request.sendJSON.stringify_bm_事件,函数{应为$document.readyfunction{