2014-04-10 44 views
0

我一直在使用Rails几年,但这是我第一次尝试过AJAX。我有一个Rails 4应用程序。我现在只是测试功能。最终,我想重新加载app/views/stories/edit.html.erb页面的部分内容,但是我无法获取$ .get()函数来执行任何操作。这里是我的应用程序/资产/ Java脚本/ story.js文件:

$(document).on("page:change", function() { 
    $.get('test.txt'); 
    timelyrefreshStories = function(){ 
    $.get('/signup'); 
    }; 
    setInterval(timelyrefreshStories, 8000); 
}); 

在哪里“的test.txt”是我的公用文件夹文件& /注册在我的路线文件的路径。 这段代码什么都不做。然而,当我将其更改为

$(document).on("page:change", function() { 
    timelyrefreshStories = function(){ 
    location.reload(true); 
    }; 
    setInterval(timelyrefreshStories, 8000); 
}); 

整个页面重新加载,并在页面重新加载大约每8+秒 - 奇怪的是,这种情况在网站上的所有网页,不只是“故事”的网页。所以我知道及时刷新的记录正在执行。

另外,我有:

gem 'jquery-rails' 
gem 'turbolinks' 
gem 'jquery-turbolinks' 
在我的Gemfile

。 为什么$ .get()不做任何事情?

回答

2

你需要告诉jQuery的做什么,当你请求的响应

$(document).on("page:change", function() { 
    $.get('test.txt',function(response){ 
    /*do something here*/ 
    }); 
    timelyrefreshStories = function(){ 
    $.get('/signup',function(response){ 
     /*do something with this other response*/ 
    }); 
    }; 
    setInterval(timelyrefreshStories, 8000); 
}); 

我不知道你到底做虽然,即JavaScript的没有什么意义,我

+0

此代码示例并不是要做任何事情,而是看它是否被执行。 – CrowdPublishTV

相关问题