当前位置: 首页 > 工具软件 > WebDAV.js > 使用案例 >

Jquery Call WebDav

董嘉祯
2023-12-01

最近研究了一下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>

 

转载于:https://www.cnblogs.com/Edwinzq/p/3761941.html

 类似资料:

相关阅读

相关文章

相关问答