【问题标题】:read data json from a specific url从特定 url 读取数据 json
【发布时间】:2022-01-20 12:39:36
【问题描述】:

我想在我的节点上显示此 url 内的数据,但它不显示任何内容,我的代码中有问题

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");

};

【问题讨论】:

    标签: javascript node.js json express try-catch


    【解决方案1】:

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

    fetch(url)
    .then((res)=> res.json())
    .then((json) => {
        let dataJson = JSON.parse(json)
    })

    【讨论】:

    • 不要忘记使用.catch(),以防请求发生任何问题。
    【解决方案2】:

    你可以用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
    }) 
    

    【讨论】:

      猜你喜欢
      • 2015-09-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-04
      • 1970-01-01
      • 2020-10-28
      • 2022-11-12
      相关资源
      最近更新 更多