首页 > 代码库 > Jquery Call WebDav

Jquery Call WebDav

最近研究了一下WebDav,尝试了一下用Jquery.ajax 发生请求访问WebDav. 代码如下:

 

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Test WebDav</title>
    <script src="Scripts/jquery-1.7.1.min.js"></script>
    <script>
        $(document).ready(function () {
            var requestData = "<?xml version=\"1.0\" ?> " +
        "<D:propfind xmlns:D=\"DAV:\">" +
                "<D:allprop/>" +
        "</D:propfind>"
            $.ajax({
                url: http://localhost/, type: "PROPFIND", contentType: "text/xml",
                data: requestData,
                dataType: "XML",
                username: "test",
                password: "test",
                headers:{depth:"1"},
                success: function (data) {
                    var filenodes = data.childNodes[0].childNodes;
                    var len = filenodes.length;
                    for (var i = 0; i < len; i++) {
                        if (filenodes[i].childNodes.length > 1 && filenodes[i].childNodes[1].childNodes.length > 1) {
                            var propStat = filenodes[i].childNodes[1].childNodes[1];
                            var displayName = propStat.getElementsByTagName("D:displayname");
                            if (displayName.length > 0) {
                                var td = "<tr><td>" + displayName[0].textContent + "</td>";
                                var modifyData = propStat.getElementsByTagName("D:getlastmodified");
                                if (modifyData.length > 0) {
                                    td = td+"<td>"+modifyData[0].textContent+"</td>";
                                }
                                var isDirectory = propStat.getElementsByTagName("D:iscollection");
                                if (isDirectory.length > 0) {
                                    td = td + "<td>"+isDirectory[0].textContent + "</td>"
                                }
                                td = td + "</tr>";
                                $("#tblBody").append(td)
                            }
                        }
                    }

                    $("#results").text(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    $("#results").text("Error: " + errorThrown)
                }
                
            });
        });
    </script>
</head>
<body>

    <textarea id="results"></textarea>
    <table>
        <thead>
            <tr>
                <th>Display Name</th>
                <th>Modified Data</th>
                <th>Is Directory</th>
            </tr>
        </thead>
        <tbody id="tblBody">
           
        </tbody>
    </table>
</body>
</html>