我有一个使用Node.js
和Express.js
的REST API。我正在使用Angular5
应用程序来使用REST API。关于从Angular应用程序进行API调用,它是通过服务完成的,代码如下:
auth.service.ts
:
login(email: string, password: string) {
return this.http.post( environment.baseUrl + '/api/v1/login', JSON.stringify({ email: email, password: password }))
.map((response: Response) => {
let user = response.json();
if (user && user.token) {
localStorage.setItem('currentUser', JSON.stringify(user));
}
});
}
在我的节点app.ts
中,应用程序定义如下:
app.ts
:
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.header('Access-Control-Allow-Origin', '*');
// Request methods you wish to allow
res.header('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.header('Access-Control-Allow-Headers', 'X-Requested-With,content-type,__setXHR_');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
res.header('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
如果你看看控制器,这里是代码:
login = (req, res) => {
console.log(req.body);
this.model.findOne({ email: req.body.email }, (err, user) => {
if (!user) { return res.sendStatus(403); }
user.comparePassword(req.body.password, (error, isMatch) => {
if (!isMatch) { return res.sendStatus(403); }
const token = jwt.sign({ user: user }, process.env.SECRET_TOKEN); // , { expiresIn: 10 } seconds
res.status(200).json({ token: token });
});
});
}
我收到403错误,以下是我的控制台上打印的内容:
[2] {}
[2] {}
[2] POST /api/v1/login 403 22.004 ms - 9
我几乎可以看到我的chrome调试器中发送了请求正文。
我在这里做错了什么?
答案 0 :(得分:0)
实际上,我在帖子请求中使用了void spread(int v, int x, int y, int *t,int w, int *count){
//table of directions(Right,Down,Left,Up)
int d[4][2] = {{1,0},{0,1},{-1,0},{0,-1}};
int i;
t[y * w + x] = v;
if(v == 0 && (x & 1) && (y & 1))
(*count)++; /*increments at every box which has value as zero with x odd and y also*/
//spread the value to all directions
for(i = 0; i < 4; i++){
if(v < t[(y+d[i][1]) * w + x +d[i][0]]){
spread(v,x + d[i][0],y+d[i][1],t,w,count);
}
}
}
int *init(int m, int n){
//Initializing the maze
int *t = NULL, mp = 2 * m +1, np = 2 * n + 1;
int x,y,k,d;
int count = 1;
t = malloc(mp * np * sizeof *t);
assert(t);
for(y = k = 0; y < np ;++y){
for(x = 0; x < mp; ++x){
if((x & 1) && (y & 1))
t[y * mp + x] = k++;
else
t[y * mp + x] = -1;
}
}
//Make a labyrinth randomly
srand(time(NULL));
while(count < (m * n)){
if(myRand(2)){ // Up/Down separator
do{
x = myRand(m) * 2 + 1;
y = (myRand(n - 1) + 1) * 2;
}while(t[(y - 1) * mp + x] == t[(y + 1) * mp + x]); /*Don't select the ones which are equal*/
d = t[(y - 1) * mp + x] - t[(y + 1) * mp + x];
//d selects the lowest one
if(d > 0){
t[y * mp +x] = t[(y + 1) * mp + x];
spread(t[(y + 1) * mp +x],x,y-1,t,mp,&count);
}
else if(d < 0){
t[y * mp +x] = t[(y - 1) * mp + x];
spread(t[(y - 1) * mp +x],x,y+1,t,mp,&count);
}
}
else{ //Right/Left separator
do{
x = (myRand(m - 1) + 1) * 2;
y = myRand(n) * 2 + 1;
}while(t[y * mp + x - 1] == t[y * mp + x + 1]);
d = t[y * mp + x - 1] - t[y * mp + x + 1];
if(d > 0){
t[y * mp +x] = t[y * mp + x + 1];
spread(t[y * mp + x + 1],x-1,y,t,mp,&count);
}
else if(d < 0){
t[y * mp +x] = t[y * mp +x - 1];
spread(t[y * mp + x - 1],x+1,y,t,mp,&count);
}
}
}
return t;
}
。我不得不发送对象而不是JSON字符串。在删除它,它工作。