在本地测试时,ajax 应用程序中的访问被拒绝
我刚刚开始阅读有关使用 ajax 进行开发的内容,并且正在本地计算机上测试网页。该页面包含的只是几行 html、javascript、css,当然还有 ajax 的请求对象等。
我正在尝试这个 - 在 page1.html 上,如果您单击其中的 tab1 (一个图标),它应该将一个名为“page2.html”的页面加载到 page1.html 中 tab1 下的内容 div 中(有点像框架)。我为此使用了ajax,大致如下:
var url= "page2.html";
request.open("GET", url, true);
request.onreadystatechange = displayResult;
request.send(null);
所以基本上,单击page1中的tab1图标将使用ajax将page2加载到page1的一部分中。运行时,它遇到了一个错误,提示访问被拒绝
,其中一行引用了request.open()
。
我知道提供 url = "page2.html"
是不够的。
问题 -
- 我如何在此处提及完整的网址,以便它可以获取 page2.html 的内容(与脚本和 page1.html 位于本地同一文件夹中)
- 我想测试我编码的页面,在我的机器本地。从上面的问题来看,我认为在本地计算机上测试 ajax 不是一个好主意。这是对的吗?我必须在服务器上测试它吗?
I've only started reading up on development using ajax, and was testing the webpage in my local machine. All that the page contains is couple of lines of html, javascript, css and of course the request objects etc for ajax.
I was trying this - on page1.html, if you click on tab1 (an icon) in it, it should load a page named "page2.html" into the content div under the tab1 in page1.html (sort of like a frame). I was using ajax for this, something roughly like:
var url= "page2.html";
request.open("GET", url, true);
request.onreadystatechange = displayResult;
request.send(null);
So basically, clicking on the tab1 icon in page1 would load page2 into a portion of the page1, using ajax. On running, it ran into an error saying Access denied
, with line referring to the request.open()
.
I know that giving url = "page2.html"
will not suffice.
Question -
- How do i mention the full url here so that it can fetch the contents of page2.html (which resides in the same folder, locally, as the script and page1.html)
- I would like to test the pages that i code, locally on my machine. From the above issue, i assume that testing ajax on a local machine is not a good idea. Is this right? DO i have to test it on a server?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你无法使用 XMLHttpRequest 获取本地文件的内容,除非你修改浏览器的配置:
在 Opera 中
在“about:config”页面上:
启用“允许文件 XMLHttpRequest”
you can't get content of local files with XMLHttpRequest, except your modify configuration of your Browser:
in Opera
on "about:config" page:
enable "Allow File XMLHttpRequest"