MongoDB를 쿼리하고 일부 문서를 가져 오려고합니다. 그런 다음 해당 문서를 클라이언트로 다시 스트리밍하여 페이지를 새로 고치지 않고 <table>
을 채 웁니다. socket.io를 사용하여이 작업을 수행하는 방법을 이미 알고 있지만 소켓없이 데이터를 전송하는 방법을 배우고 싶습니다. /loadRecent
리소스가 없기 때문에 현재 Failed to load resource: the server responded with a status of 404 (Not Found)
이 표시되고 있지만 새 페이지를로드하지 않고 GET
을 수행하는 방법을 모르겠습니다. (나는 REST가 어떻게 작동하는지에 대한 기본적인 지식을 놓치고있을 수도있다.) 조언을 부탁한다.AJAX를 사용하여 서버에서 클라이언트로 데이터를 보내는 방법은 무엇입니까?
서버 코드 :
#Get recent documents
app.get '/loadRecent', (req, res) ->
console.log 'Documents requested...'
db.collection 'documents', (err, collection) ->
collection.find().sort(dateAdded:-1) (err, cursor) ->
if not err
res.setHeader 'content-type':'application/json'
cursor.each (err, item) ->
res.write item
else
console.log 'Error getting recent docs: ' + err
클라이언트 코드 (지금이 단지 console.log
이지만, 계획이 I 데이터가 흐르는 일단 <table>
에 데이터를 추가하는 것입니다.) :
$.getJSON('/loadRecent', function(data, textStatus, jqXHR)
{
console.log('Data recieved from server: ' + data);
});