weixin_33691598 2016-02-24 14:16 采纳率: 0%
浏览 20

为什么我不能在Rails中使用Ajax?

I started learning Rails 3 months ago.And now I'm trying to add Ajax in my Rails app.

So it have some problems.

In my Index.html.erb file:

<script>
    function loadDoc() {
      var xhttp = new XMLHttpRequest();
      xhttp.onreadystatechange = function() {
        if (xhttp.readyState == 4 && xhttp.status == 200) {
          document.getElementById("demo").innerHTML = xhttp.responseText;
        }
      };
      xhttp.open("GET","demo.txt", true);
      xhttp.send();
    }
</script>

<button type="button" onclick="loadDoc()">click</button>

<p id="demo"></p>

And I created a file that demo.txt.I try to show content in demo.txt when I click the button.But it shows nothing.

So have any tut for me.

Thanks !

  • 写回答

1条回答 默认 最新

  • 胖鸭 2016-02-24 14:33
    关注

    The second parameter of the xhttp.open(...) call should be a URL, not simple a file name. If you are running the Rails application with "rails s" then your url might look something like "http://localhost:3000/demo.txt" if the file demo.txt is in your public folder. Without a URL in that second parameter, I suspect that the request is never finding your rails application.

    You might also want to dig into jquery (since you tagged it) to understand how to use that library to issue the request. The interface is much simpler than the low-level js.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog