【发布时间】:2018-02-14 10:22:23
【问题描述】:
在 vanilla JS 中,我正在向服务器发出 get 请求,以在初始页面加载后检索项目列表(对话中的消息列表)。当发送回复(post 请求)时,我想将该新消息附加到现有消息列表而不刷新整个页面(想想 React)。
这适用于页面最初加载时:
window.addEventListener('load', () => {
axios.get(get_url).then((res) => {
// Get conversation
})
})
然后,发送回复:
const sendReply = () => {
const reply = document.getElementById('replyMessage').value
axios.post(post_url, {
// data
})
.then(function(res) {
if (res.status === 201) {
window.location.reload(true)
}
})
}
如您所见,我正在刷新整个页面,感觉不太理想。应该有更好的方法将新消息附加到现有对话中。 可以用vanilla JS来做吗?
更新
如果有人好奇我是如何解决这个问题的,这就是我所做的:
const sendReply = () => {
const replyText = document.getElementById('replyMessage').value // Get the text of the reply
axios.post('/messages/api/reply', {
memberFirstName: Cookies.get('first_name'),
memberId: Cookies.get('id'),
conversationId: conversationId,
reply: replyText
})
.then(function(res) {
if (res.status === 201) {
const reply = res.data.reply
messagesArray.push(reply)
const replyRow = (copy) => {
return `
<div class="col-md-6 col-md-offset-3" id="messageList">
<div class="well">
<div align="left">
<div style="float: left">${copy.from}: <b>${copy.message}</b></div>
<div style="float: right">Unread</div>
<div style="clear: both"></div>
</div>
</div>
</div>
`
}
if (messagesArray.length === 2) { // If this is the first reply done without refreshing the page, run this block
const copy = messagesArray[1]
replySection = replyRow(copy)
} else if (messagesArray.length > 2) { // If the user decides to send more than one reply without refreshing the page, run this block
const copy = messagesArray[messagesArray.length - 1]
replySection += replyRow(copy)
}
document.getElementById('replyId').innerHTML = replySection
document.getElementById('replyMessage').value = ' '
} else {
window.alert("Error")
}
})
}
window.addEventListener('load', () => {
const url = window.location.pathname.split('/')
if (url[1] === 'conversation' && url[2] === conversationId) {
axios.get(`/conversation/api/${conversationId}`).then((res) => {
messagesArray.push(res.data.messages)
let messageList = `<center>`;
messagesArray[0].map((message) => {
messageList += `
<div class="col-md-6 col-md-offset-3" id="messageList">
<div class="well">
<div align="left">`
message.unread ? (
messageList += `
<div style="float: left">${message.from}: <b>${message.message}</b></div>
<div style="float: right">Unread</div>
<div style="clear: both"></div>
`
) : (
messageList += `
<div style="float: left">${message.from}: ${message.message}</div>
<div style="float: right">Read</div>
<div style="clear: both"></div>
`
)
messageList += `</div></div></div>`;
})
const reply = `
<div id="contactForm">
<div class="form-group col-md-6 col-md-offset-3">
<center>
<textarea class="form-control" id="replyMessage" rows="5" cols="10"></textarea>
<br />
<button class="btn btn-primary" onclick="sendReply()">Send</button>
</center>
</div>
</div>
`;
messageList += `<div id="replyId"></div>`; // The reply goes here
messageList += `</center>`;
htmlOutput += messageList + reply
document.getElementById('my-app').innerHTML = htmlOutput;
})
} else if (url[1] === 'users' && url[3] === 'about') {
}
})
【问题讨论】:
-
更新:我想通了。我只是在html代码的底部放置了一个带有
id的空白div标签,然后在回复的承诺中使用document.getElementById('replyId').innerHTML定位id。
标签: javascript post get axios