简体   繁体   English

Ruby Rails-具有AJAX POST的Javascript在本地运行,但在Heroku中不运行

[英]Ruby Rails - Javascript with AJAX POST working locally but not Heroku

I seem to be experiencing some sort of perennial problem regarding JS and Heroku. 我似乎遇到了一些有关JS和Heroku的长期问题。 Although not exactly---some of my JS works, but not all. 尽管不完全正确-我的某些JS可以工作,但不是全部。

I have the following in application.js : 我在application.js有以下内容:

//= require jquery
//= require jquery_ujs
//= require bootstrap
//= require_tree .


 function submitForm(pagenamed, starttime, endtime) {
      alert("checked the button - worked");
      $.ajax({
           type:'POST', 
           url: '/pagetimes/create', 
           data: $.param({ pagetime: {pagename: pagenamed, start: starttime, end: endtime }}),
           async:false
      });
 }

var d = new Date();
var starttime = d.getTime();
var pagename = document.URL
alert("load works START:" +d+ starttime + pagename);

window.onbeforeunload=function() {
var j = new Date();
var endtime = j.getTime();
alert("unload works START:" +d+ starttime +"END:"+j+ endtime +pagename );
      $.ajax({
           type:'POST', 
           url: '/pagetimes/create', 
           data: $.param({ pagetime: {pagename: pagename, start: starttime, end: endtime }}),
           async:false
      });
}

Which, as you may guess, is a JS to capture time spent on a page and record it under a model (pagetimes) via sending an AJAX call to the create action (all without user input). 您可能会猜到,这是一个JS,它通过向create动作发送AJAX调用来捕获花费在页面上的时间并将其记录在模型下(页面时间)(所有这些都无需用户输入)。 I am still developing so I have left a bunch of alerts in to show me when the functions get fired. 我仍在开发中,因此我已经留下了很多警报来向我展示这些功能何时被触发。 There is also a function which I can call using a button which effectively does the same thing (how I debugged the AJAX call when I built it). 我还可以使用一个按钮来调用该函数,该按钮可以有效地完成相同的功能(构建时如何调试AJAX调用)。 All of this works seamlessly in localhost (development), but only the button works in production on Heroku. 所有这些都可以在localhost(开发)中无缝运行,但是只有按钮可以在Heroku上的生产环境中使用。 And even given that, both the onload and onbeforeunload actions fire on Heroku when expected (I get the alerts, exact same as in development), but nothing shows up in my database. 甚至考虑到了这一点, onloadonbeforeunload操作在预期的时候都会在Heroku上触发(我得到的警报与开发中完全相同),但是数据库中什么都没有显示。 Any thoughts on whats going wrong here? 对这里出什么问题有任何想法吗?

There are a lot of answers out there about precompiling assets and then pushing to Heroku, or adjusting various options in the production.rb or application.rb file, have tried a bunch of those to no avail. 关于预编译资产,然后推送到Heroku,或调整production.rbapplication.rb文件中的各种选项,有很多答案,但都尝试了很多,但都无济于事。 Also some answers report ordering of the //=... lines in the application.js file matters (having something to do with bootstrap), I've tried that too but again nothing. 还有一些答案报告了application.js文件中//=...行的顺序很重要(与引导程序有关),我也尝试过,但再次没有尝试。

It must have something to do with the variables I am declaring in the event functions, or the async:none option? 它必须与我在事件函数中声明的变量或async:none选项有关吗? Otherwise I'm out of ideas. 否则我就没主意了。 Am using the same browser for both localhost and heroku app. 我正在为本地主机和heroku应用程序使用相同的浏览器。 Is there anything obvious I might be missing? 有什么明显的我可能会丢失的东西吗? Using Rails 3, Win7, no fancy gems. 使用Rails 3,Win7,没有花哨的宝石。

Update 1: 更新1:

I found the following: JavaScript not loading on Heroku but works locally which sounds very similar to my problem. 我发现以下内容: JavaScript未在Heroku上加载,但在本地运行 ,这听起来与我的问题非常相似。 Could this be it? 可以吗?

Update 2: 更新2:

I included the pagename variable in my alert to see if that wasn't working properly. 我在警报中包含了pagename变量,以查看其是否正常运行。 Not the problem, the correct info appears in the alert. 没问题,警报中会显示正确的信息。 Also made the button and onbeforeunload async options the same in case that was it. 还可以使按钮和onbeforeunload异步选项相同。 Is not. 不是。 Now everything about these ajax calls is the exact same, but one works and the other doesn't. 现在,关于这些ajax调用的所有内容都是完全相同的,但是一个有效,而另一个无效。 What gives? 是什么赋予了?

Here is the answer (for me)--nothing to do with JS, only the postgres DB. 这是(对我而言)答案-与JS无关,仅与postgres DB有关。 My integers were too big. 我的整数太大。

Integer out of range on Postgres DB Postgres DB上的整数超出范围

Heroku / Postgres Error: integer out of range Heroku / Postgres错误:整数超出范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM