2015-01-08 3 views
0

내 DB에 컬렉션을 삭제하려면 다음 코드를 사용하고 삭제 아약스에 다시 성공 호출을 사용하는 방법 :[Node.js를]

클라이언트 :

$('.destroy').click(function() { 

    if(confirm("Are u sure?")) { 
     $.ajax({ 
      type: 'DELETE', 
      url: '/destroy/' + dataId, 
      success: function(response) { 
       console.log('Success'); 
      } 
     }); 
    } else { 
     alert('Cancelled'); 
    } 
}); 

서버 :

내가 파괴 버튼을 클릭하고 페이지를 다시로드하면
app.get('/destroy/:id', function(req, res) { 
    var id = req.param("id"); 

    MyModel.remove({ 
     _id: id 
    }, function(err){ 
     if (err) { 
      console.log(err) 
     } 
     else { 
      console.log('Collection removed!'); 
     } 
    }); 
}); 

는 컬렉션이 없을 것, 작업이지만, 함께 성공 콜백 함수 : [console.log('Success');]는 ..

01를 실행하지 않는

성공 함수를 실행하려면 서버에서 클라이언트로 콜백을 보내야합니다 ???

console.log ('성공')을 만드는 방법; 운영??

감사합니다.

답변

3

아약스 호출은 아마도 서버에서 응답을 다시받지 못하기 때문에 아마 시간이 초과 될 것입니다.

는 그 다음

$.ajax({ 
    type : 'DELETE', 
    url  : '/destroy/' + dataId, 
    success : function(response) { 

     if (response === 'error') { 

      alert('crap!'); 

     } else if (response === 'success') { 

      alert('worked fine!'); 

     } 

    } 
}); 

, 당신은 당신이 원하는대로 돌아 statusCodes 또는 무엇이든을 보낼 수 이것은 간단한 예이다 잡을 서버

app.get('/destroy/:id', function(req, res) { 
    var id = req.param("id"); 

    MyModel.remove({ 
     _id: id 
    }, function(err){ 
     if (err) { 
      res.end('error'); 
     } 
     else { 
      res.end('success'); 
     } 
    }); 
}); 

에서 응답을 보냅니다.

+0

감사합니다. 8 분 안에 당신의 대답을 받아 들일 것입니다! :) –

+0

@ mr.BearAndBeer - 여러분을 환영합니다! – adeneo