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