javascript 从特定的url读取数据json

ymzxtsji  于 2023-05-27  发布在  Java
关注(0)|答案(2)|浏览(176)

我想做的显示数据内的网址对我的节点站出来,但它显示没有,有一些我做错了我的代码

const extractRefDefaultSchema = async (data) => {
const url = "https://mos.esante.gouv.fr/NOS/TRE_R81-Civilite/FHIR/TRE-R81-Civilite/TRE_R81-Civilite-FHIR.json"
https.get(url,(res) => {
    let body = "";
    res.on("data", (chunk) => {
        body += chunk;
    });
    res.on("end", () => {
        try {
            let json = JSON.parse(body);
            // do something with JSON
        } catch (error) {
            console.error(error.message);
        };
    });
}).on("error", (error) => {
    console.error(error.message);
});
console.log("extractRefDefaultSchema");

};

icomxhvb

icomxhvb1#

您可以使用fetch API来执行以下操作:

fetch(url)
.then((res)=> res.json())
.then((json) => {
    let dataJson = JSON.parse(json)
})
.catch((err) => {
  console.log(err)
})
e0bqpujr

e0bqpujr2#

你可以用axios实现同样的功能,它会简单得多。
代码:

let axios = require("axios")
axios.get("https://mos.esante.gouv.fr/NOS/TRE_R81-Civilite/FHIR/TRE-R81-Civilite/TRE_R81-Civilite-FHIR.json").then((res) => {
    let jsonData = res.data
})

相关问题