AJAX XMLHTTP 请求

发布于 2024-10-18 03:27:02 字数 848 浏览 2 评论 0原文

我创建了一个 mozilla 扩展,它是浏览器上的一个按钮。该按钮有一个 javascript,单击该按钮时应发送 XMLHTTLP 请求。我想使用我在 URL 字段中创建的本地 HTML 文件。当我使用它时,我仍然无法查看该 HTML 页面。为什么会这样呢?代码如下:

CustomButton = { 

1: function () 
  {

     var xmlhttp;
     if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
     }
     else
     {// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function()
     {
         if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
             document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
         }
     }
     xmlhttp.open("GET","http://localhost/sample.html",true); 
     xmlhttp.send();
  }
} 

sample.html 文件位于 xampp 的 htdocs 文件夹中。

I have created a mozilla extension which is a button located on the browser. This button has a javascript which when clicked should send a XMLHTTLP request. I want to use a local HTML file which I have created in the URL field of it. When I use it I still can't view that HTML page. Why is that so? The code goes like this:

CustomButton = { 

1: function () 
  {

     var xmlhttp;
     if (window.XMLHttpRequest)
     {// code for IE7+, Firefox, Chrome, Opera, Safari
         xmlhttp=new XMLHttpRequest();
     }
     else
     {// code for IE6, IE5
         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
     }
     xmlhttp.onreadystatechange=function()
     {
         if (xmlhttp.readyState==4 && xmlhttp.status==200)
         {
             document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
         }
     }
     xmlhttp.open("GET","http://localhost/sample.html",true); 
     xmlhttp.send();
  }
} 

The sample.html file is located in the htdocs folder of xampp.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦年海沫深 2024-10-25 03:27:02

出于安全原因,不允许使用 XMLHttpRequest 访问本地文件。

Accessing local files with XMLHttpRequest is not allowed for security reasons.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文