【问题标题】:Using jquery .load() in Rails在 Rails 中使用 jquery .load()
【发布时间】:2013-10-30 12:38:54
【问题描述】:

我想从.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: 我实际上想访问我的应用程序公共文件夹中的 HTML 文件。 路径是这样的.. RORapp/public/reports///.html 文件

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

更新2:

当我只是复制 url(http://localhost:3000/reports/"+userid+"/"+fold+"/overview.html) 并在浏览器中打开时,我可以看到 html 页面。但是当我尝试加载其中的一部分时,我无法做到。 cmd ,我得到一个错误提示

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

account 是我的主控制器,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**

}

【问题讨论】:

  • 您很可能需要使用服务器。我检查了 javascript 控制台是否有错误 - 我猜你会看到一个关于“跨域策略不允许”
  • 我正在使用 webrick 服务器.. 我正在我的机器上启动它.. 并以 localhost:3000 身份访问应用程序
  • 那么您到sample.txt 的路径应该类似于http://localhost:3000/path/to/sample.txt(或者是相对路径)
  • 哦,是的!让我试着回来..
  • 酷,很高兴你把它整理出来。您可以以相同的方式加载 HTML。请记住在下面接受 Rory 的回答,以确认问题已得到解决。

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


【解决方案1】:

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

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

【讨论】:

  • 我正在使用 webrick 服务器..我正在我的机器上启动它..并以 localhost:3000 身份访问应用程序..我是 ROR 的新手..你能解释一下需要什么吗完成??当我在普通的 .html 文件中使用相同的代码时,.load() 函数确实有效..
  • 您需要通过本地服务器上的 URL 访问sample.txt,例如。 http://localhost:3000/folder/sample.txt 甚至是根的相对路径:/folder/sample.txt。问题是因为您使用的是本地 URI(D: 驱动器)。
  • 我做到了,但它不起作用..我需要访问我的 ROR 应用程序的公共文件夹中的 html 文件..(public/reports/1/overview.html) 如何获取路径为了那个原因??我试过这个 $("#quickview").load("localhost:3000/../../sample.txt"); 和 $("#quickview").load("localhost:3000/reports/"+userid+"/"+fold+"/… #overviewTable");
  • 是的,我已经给出了 http://.. 在这里创建链接时它没有显示。我的网址对吗?
  • 在这种情况下检查控制台是否有错误(在浏览器中按 F12)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多