简体   繁体   English

在rails上的ruby中调用auto ajax是行不通的

[英]auto ajax call in ruby on rails is not working

I'm trying to do this: after my homepage loads (home.html.erb), call ajax to update the value of a tag on the same page. 我正在尝试这样做:在我的主页加载(home.html.erb)之后,调用ajax来更新同一页面上的标记值。 Here is what I have in my home.html.erb 这是我home.html.erb中的内容

<script>
  $(document).ready(function() {
    $.ajax({
      type: 'GET',
      url: '<%= url_for(action: "home") %>'
    });
  });
</script>

and my home.js.erb 和我的home.js.erb

var id = 5;
$('#jquerylogo').html(id);

and my controller 和我的控制器

def home
  respond_to do |format|
    format.html # home.html.erb
    format.js
  end
end

but ajax is not called and the code doesn't work. 但是没有调用ajax并且代码不起作用。 why? 为什么? thanks. 谢谢。

Sometimes ujs issues are tricksie. 有时ujs问题是技巧。

I would start by adding a dataType to the ajax call to force the controller to respect it's type. 我首先将一个dataType添加到ajax调用,以强制控制器遵守它的类型。

$(document).ready(function() {
  $.ajax({
    type: 'GET',
    dataType: 'script',
    url: '<%= url_for(action: "home") %>'
  });
});

Things to look for are: 要寻找的是:

  • Check the javascript console to see what (if any) errors you have 检查javascript控制台,看看你有什么(如果有的话)错误
  • Check your log to see if the request is made/received/rendered as html 检查您的日志,看看是否以html格式发出/接收/呈现请求

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

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