0
이것은 분명히 알려진 문제이지만 내 MEAN 프로젝트에서이를 수정하는 방법을 모릅니다. 여러 요청이 전송 된 것으로 보입니다. 찾을 수있는 모든 솔루션을 시도했습니다 (이전 버전 또는 최신 버전). 지금까지 성공하지 못했습니다.Mongojs 보낸 후 헤더를 설정할 수 없습니다.
res.end() res.redirect는 ('/')
아무것도 트릭을 할 것 같다.
이 내 HTTP 방법
//delete
router.delete('/task/:id', function(req, res, next) {
let post = mongojs.ObjectId(req.params.id)
db.tasks.remove({_id: mongojs.ObjectId(req.params.id)}, function(err, task){
res.status(200).res.json(task)
return res.end()
console.log('SERVER - post ' + req.body.title + ' successfully deleted')
if (err) {
res.send('error message ' + err)
}
})
})
//update
router.put('/task/:id', function(req, res, next) {
var task = req.body
var updateTask = {}
updateTask.isDone = task.isDone
updateTask.title = task.title
if(!updateTask) {
res.status(404).send({
"error": "bad request"
})
} else {
db.tasks.update({_id: mongojs.ObjectId(req.params.id)}, updateTask, {}, function(err, task){
res.status(200).res.json(task)
return res.end()
if (err) {
res.send('error message ' + err)
}
})
}
})
이 각도 응용 프로그램입니다을 처리하는 곳, 그래서 여기에 내 코드 내 서비스 파일입니다. 하지만 클라이언트와 관련된 문제는 없습니다.
public deletePost(post: Post) {
console.log('SERVICE - post ' + post._id + ' successfully deleted')
return this.http.delete('http://localhost:1234/api/task/' + post._id)
.map(res => res.json)
}
public updatePost(postId : string, newText:string) {
}
public addPost(newPost: Post) {
var headers = new Headers();//??
headers.append('Content-Type', 'application/json');
console.log('successfully added (service)' + newPost.title)
let body = JSON.stringify({title: newPost.title, isDone:false});
console.log(body)
return this.http.post('http://localhost:1234/api/task', JSON.parse(body),{headers})
.map(res => res.json());
}
가 해결! 클라이언트와 서버에서 실제로 두 번 요청을 보냈습니다. – SandyL
이 (가) 대체되었습니다. res.status (200) .res.json (task) return res.end() – SandyL