reciveMdtext async RequestMdtext axiosgethttplocalhost8080getOneChapter params userId 1 tutorialId 2 then
The code you provided seems to be a JavaScript code snippet that includes a function called RequestMdtext which uses the Axios library to make an HTTP GET request to a specific endpoint (http://localhost:8080/getOneChapter).
Here is the code with some improvements and explanations:
async function RequestMdtext() {
try {
const res = await axios.get('http://localhost:8080/getOneChapter', {
params: {
userId: 1,
tutorialId: 2,
},
});
const previewElement = document.getElementById('MarkDownDoc');
const html = await Vditor.md2html(res.data.data);
previewElement.innerHTML = html;
} catch (error) {
console.error(error);
}
}
Improvements and explanations:
- The function is now declared as an
asyncfunction, allowing the use ofawaitinside it. - The
axios.getrequest is now wrapped in atry-catchblock to handle any potential errors. - The response object is directly assigned to the
resvariable usingawaitto asynchronously wait for the response. - Instead of using the
.thenmethod, the response handling code is now placed directly after theawaitstatement. - The
previewElementis obtained usingdocument.getElementByIdto get the element with the ID'MarkDownDoc'. - The
htmlvariable is now awaited to asynchronously wait for the conversion of Markdown to HTML usingVditor.md2html. - The HTML content is assigned to the
innerHTMLproperty ofpreviewElementto update the DOM and display the HTML.
Please note that this code assumes that you have the necessary libraries (axios and Vditor) imported and available in your code environment
原文地址: https://www.cveoy.top/t/topic/i0yJ 著作权归作者所有。请勿转载和采集!