HI 내가 HTML 아약스 호출하여 MongoDB를 사용하여 내 HTML로HTML 아약스 호출 노드 MongoDB를이
클라이언트 HTML (dbajax.html를) 결과를 채우는 싶습니다 실패
http.createServer(function (request,response)
{
// serve site
if (request.url === "/")
{
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
}
if (request.url === "/db")
{
console.log("db");
MongoClient.connect("mongodb://localhost:27017/MyDb2", function (err, db) {
db.collection('Persons', function (err, collection) {
collection.find().toArray(function(err, items) {
if(err) throw err;
console.log(items);
response.writeHead(200, {"Content-Type:": "application/json"});
var submittedPost = {};
submittedPost['message'] = 'Proof that Node and Mongo are working..';
response.write("_wrapper('");
response.write(JSON.stringify(items));
response.write("')");
response.end();
});
});
});
}
if (request.url === "/dbcall"){
console.log("dbcall");
fs.readFile('./dbajax.html', function (err, html)
{
//response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}
)
}
//response.end();
}).listen(9000);
내가
http://xx.xx.xx.xx:9000/dbcall을 입력 그것은 dbajax.html 아무것도하지만 더 일어날 호출합니다.
html 아약스가 http://xx.xx.xx.xx:9000/db을 호출하고 JSON 결과를 반환한다고 가정합니다.
그래서 무엇이 잘못 되었나요? 저는 Express와 다른 프레임 워크를 사용하고 싶지 않습니다. 감사합니다.
안녕하세요. 그렇지만 내 코드의 논리가 정확하다고 생각하십니까? 나는 node.js에서 아주 새롭기 때문에 올바른 방법인지 아닌지 확실하지 않습니다. – user1042911
일반적으로 논리는 정상입니다. 나는 당신이 당신의 신청서의 발췌 부분만을 주었다고 생각합니다. 'submittedPost'의 일부분은 불필요한 것으로 간주됩니다. 그리고 만약 당신이 정말로 급행 및 기타 유용한 프레임 워크를 사용하고 싶지 않다면 그것은 갈 길입니다. – Marc
데이터 유형을 'text'로 변경하면 갑자기 작동합니다. 어째서? 유형 : 'get', dataType : 'text', jsonp : 'text', – user1042911