阿贾克斯不工作的时候,我读本地文件 [英] Ajax not working when i read a local file

查看:84
本文介绍了阿贾克斯不工作的时候,我读本地文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我解决不了这个问题连续2天。这里这examlpe: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_xml2 只是正常的W3Schools的网站。但是,当我复制并粘贴code。在记事本++。它不工作。我已经下载了XML文件。比我在读了AJAX不支持工作,本地文件。我没有得到这个?我有一个assingment学校与当地的XML文件中工作。我该如何与当地XML文件和AJAX AJAX时,不支持本地文件的工作。而我从助教得到的唯一回应是,我应该使用Firefox ...但没有..这是行不通的:Chrome浏览器,IE浏览器,Mozila,歌剧.. basicaly什么...

我知道,类似的事情已被要求在这里,但我就是不能使它工作。如果任何人有任何想法我怎么能修改此给出code工作localy,我将感谢..在此先感谢。

 <!DOCTYPE HTML>
< HTML>
    < HEAD>
        <脚本>
            函数loadXMLDoc(URL){
                VAR XMLHTTP;
                VAR TXT,X,XX,我;
                如果(window.XMLHtt prequest){// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
                    XMLHTTP =新XMLHtt prequest();
                }其他{// code对IE6,IE5
                    XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
                }
                xmlhttp.onreadystatechange =功能(){
                    如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200){
                        TXT =<表格边框='1'>< TR><第i个标题< /第i个百分位>艺术家< /第i;< / TR>中;
                        X = xmlhttp.responseXML.documentElement.getElementsByTagName(CD);
                        对于(i = 0; I< x.length;我++){
                            TXT = TXT +< TR>中;
                            XX = X [i]于.getElementsByTagName(「TITLE」); {
                                尝试 {
                                    TXT = TXT +< TD>中+ XX [0] .firstChild.nodeValue +&所述; / TD>中;
                                } 捕手) {
                                    TXT = TXT +< TD>< / TD>中;
                                }
                            }
                            XX = X [i]于.getElementsByTagName(ARTIST); {
                                尝试 {
                                    TXT = TXT +< TD>中+ XX [0] .firstChild.nodeValue +&所述; / TD>中;
                                } 捕手) {
                                    TXT = TXT +< TD>< / TD>中;
                                }
                            }
                            TXT = TXT +< / TR>中;
                        }
                        TXT = TXT +< /表>;
                        的document.getElementById('txtCDInfo)的innerHTML = TXT。
                    }
                }
                xmlhttp.open(GET,URL,真正的);
                xmlhttp.send();
            }
        < / SCRIPT>
    < /头>

    <身体GT;
        < D​​IV ID =txtCDInfo>
            <按钮的onclick =loadXMLDoc(cd_catalog.xml')>获取CD信息< /按钮>
        < / DIV>
    < /身体GT;
< / HTML>
 

解决方案

您无法与本地文件的工作。本地文件系统,沙箱,这样,比如你不能双击一个HTML文档中的电子邮件附件,在浏览器中打开它,有它你的私人文件上传到发件人的服务器。

如果你想在本地测试阿贾克斯,然后通过它安装一个本地网络服务器和测试。

Okay i cannot solve this problem for 2 days straight.. This examlpe here: http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_xml2 works just fine on the w3schools site. But when i copy and paste the code in notepad++. It doesn't work. I have the XML file downloaded. Than i was reading that AJAX doesn't support work with local files. I don't get this?? I have an assingment for school to work with a local XML file. How can i work with local XML file and AJAX when AJAX doesn't support working with local files. And the only response that i get from the teaching assistant is that i should use firefox... But No.. it doesn't work on : Chrome, Internet Explorer, Mozila, Opera.. basicaly nothing...

I know that something similar has been asked here but i just can't make it work. If anyone got any idea how can i modify this given code to work localy, i would be thankful.. THANKS in advance..

<!DOCTYPE html>
<html>
    <head>
        <script>
            function loadXMLDoc(url) {
                var xmlhttp;
                var txt, x, xx, i;
                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) {
                        txt = "<table border='1'><tr><th>Title</th><th>Artist</th></tr>";
                        x = xmlhttp.responseXML.documentElement.getElementsByTagName("CD");
                        for (i = 0; i < x.length; i++) {
                            txt = txt + "<tr>";
                            xx = x[i].getElementsByTagName("TITLE"); {
                                try {
                                    txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                                } catch (er) {
                                    txt = txt + "<td> </td>";
                                }
                            }
                            xx = x[i].getElementsByTagName("ARTIST"); {
                                try {
                                    txt = txt + "<td>" + xx[0].firstChild.nodeValue + "</td>";
                                } catch (er) {
                                    txt = txt + "<td> </td>";
                                }
                            }
                            txt = txt + "</tr>";
                        }
                        txt = txt + "</table>";
                        document.getElementById('txtCDInfo').innerHTML = txt;
                    }
                }
                xmlhttp.open("GET", url, true);
                xmlhttp.send();
            }
        </script>
    </head>

    <body>
        <div id="txtCDInfo">
            <button onclick="loadXMLDoc('cd_catalog.xml')">Get CD info</button>
        </div>
    </body>
</html>

解决方案

You can't work with local files. The local file system is sandboxed so that, for example, you can't double click an HTML document in an email attachment, open it in your browser and have it upload your private files to the sender's server.

If you want to test Ajax locally, then install a local webserver and test through it.

这篇关于阿贾克斯不工作的时候,我读本地文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆