1. 程式人生 > >使用jq的ajax實現對xml文件的讀取

使用jq的ajax實現對xml文件的讀取

nco stat html post not Coding charset doctype HA

之前一直在用json來傳遞數據,但是xml也是不可缺少的,於是開始了xml的征程。xml的一些屬性啊之類的在菜鳥教程上列舉的已經很詳細了,但是卻沒有前段部分的獲取教程,查詢資料,遂懂:

index.xml:

<?xml version="1.0" encoding="UTF-8"?>
<note>
    <to>fangMing</to>
    <from>Tom</from>
    <heading>head</heading>
    <body>
        this is body
    </body>
</note>

  index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="http://cdn.bootcss.com/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>

</body>
<script>
    $(() => {
        $.ajax({
            method: "GET",
            url: "index.xml",
            dataType: "xml",
            success: (xmlDoc, textStatus) => {
                textStatus === "success"&&(console.log($(xmlDoc).find("to").text()));

            }
        })
    })
</script>
</html>

  結果:技術分享圖片

使用jq的ajax實現對xml文件的讀取