【问题标题】:How do I read the contents of a .dat file in javascript?如何在 javascript 中读取 .dat 文件的内容?
【发布时间】:2015-12-24 15:15:48
【问题描述】:

我有一个 .dat 文件,其中填充了我的程序所需的数字行,但到目前为止,除了在代码运行后输入它之外,我无法通过任何方法访问该文件。我最近的尝试是下面的代码,但它返回了错误:

未捕获的安全性错误:无法从“HTMLIFrameElement”读取“contentDocument”属性:阻止具有“null”来源的框架访问具有“null”来源的框架。协议、域和端口必须匹配。

在行 var text

var iframe = document.createElement('iframe');
iframe.id = 'iframe';
iframe.style.display = 'none';
document.body.appendChild(iframe);
document.domain = "yourdomain.com";
iframe.src = 'hw2_files/example.dat.js';
setTimeout(function(){
    var text = document.getElementById('iframe').contentDocument.body.firstChild.innerHTML;
    alert(text);
}, 1000);

【问题讨论】:

    标签: javascript html getelementbyid appendchild createelement


    【解决方案1】:

    我认为您应该使用 AJAX 请求加载它。

    http://www.w3schools.com/jquery/ajax_get.asp

    或者是其他地方的url:

    Get JSON data from external URL and display it in a div as plain text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-17
      • 2020-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多