javascript - 在 Rails 中使用 jquery .load()

标签 javascript jquery html ruby-on-rails jquery-load

我想从 .html 文件中获取表格并将其显示在我的 .html.erb 文件中的 div 中...我尝试加载一个简单的.txt 文件如下,但现在可以使用。请让我知道要进行哪些更改。我给出的路径错了吗?

app/view/account/show_result.html.erb:

<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script type="text/javascript">

    function showy(count){
         alert(count);
        $("#quickview").load("D:\sample.txt");
        return true;
     }

</script>
</head>
<body>
<h3>Recent Test Results</h3><br>
<div id ="quickview">
    HELLO
</div>
// for loop with index i
<a id="repo" href="" onclick="showy(<%= i %>)" >Quick view!</a>
//some other code
</body>
</html>

更新1: 我实际上想访问我的应用程序的公共(public)文件夹中的 HTML 文件。 路径是这样的.. RORapp/public/reports///.html 文件

      $("#quickview").load("http://localhost:3000/reports/"+userid+"/"+fold+"/overview.html #overviewTable");

更新2:

当我复制网址(http://localhost:3000/reports/"+userid+"/"+fold+"/overview.html)并在浏览器中打开时,我可以看到html 页面..但是当我尝试加载其中的一部分时,我无法做到这一点..在 cmd 中,我收到一条错误消息

AbstractController::ActionNotFound (The action 'reportng' could not be found for AccountController):

account 是我的主 Controller ,reportng 是用于该 html 页面样式的 css 文件

在检查控制台是否有错误时,我收到以下错误(在浏览器中按 F12 时)

TypeError: element.dispatchEvent is not a function
fire()protot...?body=1 (line 4072)
element = Object[Document show_test_results]
eventName = "dom:loaded"
memo = undefined
_methodized()protot...?body=1 (line 257)
fireContentLoadedEvent()protot...?body=1 (line 4106)
[Break On This Error]   

element.dispatchEvent(event);

解决方案: 现在除了加载 reqd html 文件之外,我还可以更改属性:)

    function showy(count){
        var linkDiv = $('#repo_'+count);
        var fold = linkDiv.data('folder');
        var userid = '<%= self.current_user.id %>';
        var server = '<%= request.host_with_port %>';

        $( "#success_"+count ).load( "http://"+server+"/reports/"+userid+"/"+fold+"/overview.html #overviewTable" , function(){
            $("#success_"+count).find("a").attr("href","http://"+server+"/reports/"+userid+"/"+fold+"/suite1_test1_results.html");
        });

         **SOME CODE**

}

最佳答案

出于安全原因,浏览器会阻止对客户端计算机本地位置的 AJAX 调用。

要使其工作,您需要将其放在网上,或使用本地服务器,例如:http://localhost/mysite

关于javascript - 在 Rails 中使用 jquery .load(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19682490/

相关文章:

javascript - 无法在html表格中显示json数组

javascript - 为什么我的 <a> 的名称没有随着 for 循环而改变?

javascript - ES5 中通过回调查找第一个匹配元素

javascript - D3 在格式化时间时添加 19 小时

jquery - 使用jquery选择大于n的表列?

javascript - 当我将位置更改为固定时,为什么我的导航向左浮动?如何解决该问题并使其居中?

jquery - 悬停时的星级评分系统

javascript - 如何让div元素随页面滚动

javascript - 点击功能的问题

javascript - Sitecore 管道上传处理器