Javascript 如何在haml视图中运行特定脚本?
所以我试图调用一个Javascript 如何在haml视图中运行特定脚本?,javascript,ruby-on-rails,ajax,polling,Javascript,Ruby On Rails,Ajax,Polling,所以我试图调用一个pollServer()haml视图中的脚本,但它未运行。以下是方法: %div{:id => 'ajaxurl', :class => @poll.id} :javascript $(document).ready(function(){ pollServer(); }); 以下是pollServer函数: $(document).ready(function(){ console.log('here i am');
pollServer()代码>haml视图中的脚本,但它未运行。以下是方法:
%div{:id => 'ajaxurl', :class => @poll.id}
:javascript
$(document).ready(function(){
pollServer();
});
以下是pollServer函数:
$(document).ready(function(){
console.log('here i am');
function pollServer(){
console.log('enter pollServer');
setInterval(function(){
console.log('enter setInterval');
var id = $('div#ajaxurl').attr('class');
$.ajax({
// Div class dynamically set to poll.id
type: 'GET',
dataType: 'json',
url: '/polls/' + id,
complete: function(data){
console.log('pollServer complete');
updateSurvey(data);
},
success: function(data){
console.log('pollServer success');
updateSurvey(data);
},
error: function(){
console.log('pollServer error');
}
});
}, 4000);
};
});
以下是PollsController处理请求的show操作:
def show
@poll = Poll.find(params[:id])
# using for showTotalVotes
gon.votes = @poll.questions[0].answers.map{|answer| answer.votes}
# gon.poll_ids = @poll.questions[0].answers.map{|answer| answer.id}
gon.titles = @poll.questions[0].answers.map{|answer| answer.title}
# gon.poll_data = [gon.poll_ids, gon.titles , gon.votes ]
# => gon.answer = @poll.questions[0].answers
# gon.poll_hash = @poll.questions[0].answers.map{|answer| answer = {:id=> answer.id, :title => answer.title, :votes => answer.votes} }
@question = @poll.questions[0]
@answers = @question.answers
gon.answers = @poll.questions[0].answers
respond_to do |format|
format.html { @poll}
format.js {
render json: gon.answers
}
end
奇怪的是,我的服务器日志实际上轮询了我的rails服务器,并显示sql db查询在指定的ajax路径上正确地检索数据,但它不会控制台.log('hi'),也不会在ajax成功时调用updateSurvey方法。你知道为什么会这样吗
更新
以下是pollServer函数应该调用的updateSurvey函数
function updateSurvey(all_data){
console.log('balls');
var svg = d3.select('svg')
var bars = svg.selectAll('rect')
.data(all_data)
.transition()
.duration(500)
.attr("y", function(d){
return h + (yOffset - yScale(d.votes))
// return h-(d.votes*10);
})
.attr("height", function(d){
barHeight = yScale(d.votes);
return barHeight;
})
}
我认为您的服务器返回的json内容类型不正确。
我运行以下代码:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script>
function pollServer(){
setInterval(function(){
console.log('hi');
$.ajax({
type: 'GET',
dataType: 'json',
url: 'http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=true',
success: function(data){
console.log('hi');
updateSurvey(data);
}
});
}, 4000);
};
$(document).ready(function()
pollServer();
});
</script>
这可以解释这两种情况——为什么js代码没有运行,而服务器端的SQL却运行。如果您的源代码与上面的完全相同,那么它不会记录“hi”,因为您编写了console.log(hi),所以它会尝试记录变量。接下来的所有js可能都不能正常运行。我也非常确定您应该将函数名传递给setInterval,而不是匿名函数。相反,我将编写$(document).ready(函数(){setInterval(pollServer,4000);})代码>好吧,谷歌搜索一下,我对匿名函数的看法显然是错的。上面的代码仍然没有记录hi。剩下的看起来不错。很酷,谢谢你的提示!将(hi)更改为('hi'),但它仍然没有调用updateSurvey函数。此应用程序的行为非常奇怪…您的服务器是否返回正确的内容类型?当我在google上测试它时,我需要将success
更改为complete
以获得ReferenceError:找不到变量:updateSurvey
有趣,您是如何运行它的?我在另一个文件中有updateSurvey函数。如果您在没有它的情况下运行代码(我将在稍后发布),这可能就是它找不到它的原因。因为当我呈现触发脚本的视图时,我没有得到那个错误(或任何错误)。看看这个评论-谢谢我的帮助!这无疑是通向正确的解决方案,但它仍然没有进入函数调用。例如,在更新后的pollServer中,它只在控制台上记录“我在这里”,因为应用程序仍然每四秒钟从服务器检索一次信息,尽管没有进入该pollServer,它一定是在应用程序的其他地方被调用的。试着在ajax中添加一个complete
块,看看是否有响应。如果有任何错误,你应该能够捕捉到
hi
ReferenceError: Can't find variable: updateSurve