1
我想通過服務進行更新。如果我使用與服務中鏈接相同的郵遞員,更新可以正常工作。意味着更新不能從服務,只從郵遞員
這裏是我的代碼:
myservice.service.ts:
updateUser(userid){
let headers = new Headers();
headers.append('Content-Type','application/json');
console.log('http://localhost:3000/users/update/'+ userid);
return this.http.put('http://localhost:3000/users/update/'+ userid,
{headers: headers}).map(res => res.json());
}
內航線/ users.js
router.put('/update/:id', (req, res) => {
User.findByIdAndUpdate(req.params.id, {
$set: {
participates: true}
},
{
new: true
},
function(err, updatedUser){
if(err){
res.send("Error updating user");
} else{
res.json(updatedUser);
}
}
);
});
我沒有得到一個錯誤,但它不似乎進入router.put。 我錯過了什麼?
你有CORS標題嗎? – Claies
[How to allow CORS?](https://stackoverflow.com/questions/7067966/how-to-allow-cors) – georgeawg
我使用'@ angular/http'中的import {Http,Headers};我使用const cors = require('cors'); app.use(cors());在我的index.js文件中 – Ivett