向 Express 服务器发出多个 AngularJS Post 请求?服务器在第二个 post 请求时崩溃
Making multiple AngularJS Post requests to an Express Server? Server crashes on second post request
鉴于我无法在我的客户端中同时 运行 这两个 post 请求,我正在尝试 运行 第二个 post在第一个 post 的 .then
部分。这在我的其他项目中一直运行良好。但是由于某种原因,当第二个 post 请求触发时,我的服务器没有回复。当我检查服务器的控制台时,我注意到它崩溃了并且有一条错误消息(在 post 的底部)。
可能是什么原因造成的???
我已经在服务器代码中的第二个 post 请求上设置了断点,并且注意到断点甚至没有被击中。服务器在点击并让我选择继续之前崩溃。
客户端代码(当用户按下按钮时触发):
$scope.searchCharacter = function(){
var request = {name: $scope.charName, realm: $scope.selectedRealm};
//First post request
$http.post('/searchCharacter', request)
.then(function(response) {
//sets some variables
var id = 0;
//Second post request
$http.post('/helloworld', id)
.then(function(response) {
//sets some more variables
debugger;
});
});
}
服务器代码:
//First post request
app.post('/searchCharacter', jsonParser, function (req, res) {
blizzard.wow.character(['profile', 'stats', 'items', 'statistics'], { origin: 'us', realm: req.body.realm.name, name: req.body.name })
.then(response => {
if(response.status != 200){
res.send("That character doesn't exist! Please enter a valid character name.");
} else {
console.log(response.data);
res.send(response.data);
}
});
});
//Second Post Request
app.post('/helloworld', jsonParser, function (req, res) {
console.log(req.body);
res.send("hello");
});
错误信息:
SyntaxError: Unexpected token #
at Object.parse (native)
at createStrictSyntaxError
(c:\Users\RDubz\Documents\Interviews\EagleDream
12-7-17\Project\node_modules\body-parser\lib\types\json.js:157:10)
at parse (c:\Users\RDubz\Documents\Interviews\EagleDream
12-7-17\Project\node_modules\body-parser\lib\types\json.js:83:15)
at c:\Users\RDubz\Documents\Interviews\EagleDream
12-7-17\Project\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (c:\Users\RDubz\Documents\Interviews\EagleDream
12-7-17\Project\node_modules\body-parser\node_modules\raw-body\index.js:224:16)
at done (c:\Users\RDubz\Documents\Interviews\EagleDream
12-7-17\Project\node_modules\body-parser\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd
(c:\Users\RDubz\Documents\Interviews\EagleDream
12-7-17\Project\node_modules\body-parser\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:67:13)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
Facepalm 我使用 var id = 0 并将其传递给我的函数,但没有意识到它需要作为对象或参数传递。感谢评论的人!
试试这个:
$scope.searchCharacter = function(){
var request = {name: $scope.charName, realm: $scope.selectedRealm};
//First post request
$http.post('/searchCharacter', request)
.then(function(response) {
//sets some variables
var id = 0;
//Second post request (append id to route).
$http.post('/helloworld/' + id)
.then(function(response) {
//sets some more variables
debugger;
});
});
}
//First post request
app.post('/searchCharacter', jsonParser, function (req, res) {
blizzard.wow.character(['profile', 'stats', 'items', 'statistics'], { origin: 'us', realm: req.body.realm.name, name: req.body.name })
.then(response => {
if(response.status != 200){
res.send("That character doesn't exist! Please enter a valid character name.");
} else {
console.log(response.data);
res.send(response.data);
}
});
});
//Second Post Request (get id from req.params.id)
app.post('/helloworld/:id', function (req, res) {
console.log(req.params.id);
res.send("hello");
});
它将 id
附加到 helloworld
请求,并使用 req.params.id
定义路由 helloworld/:id
以从请求中提取 ID。
鉴于我无法在我的客户端中同时 运行 这两个 post 请求,我正在尝试 运行 第二个 post在第一个 post 的 .then
部分。这在我的其他项目中一直运行良好。但是由于某种原因,当第二个 post 请求触发时,我的服务器没有回复。当我检查服务器的控制台时,我注意到它崩溃了并且有一条错误消息(在 post 的底部)。
可能是什么原因造成的???
我已经在服务器代码中的第二个 post 请求上设置了断点,并且注意到断点甚至没有被击中。服务器在点击并让我选择继续之前崩溃。
客户端代码(当用户按下按钮时触发):
$scope.searchCharacter = function(){
var request = {name: $scope.charName, realm: $scope.selectedRealm};
//First post request
$http.post('/searchCharacter', request)
.then(function(response) {
//sets some variables
var id = 0;
//Second post request
$http.post('/helloworld', id)
.then(function(response) {
//sets some more variables
debugger;
});
});
}
服务器代码:
//First post request
app.post('/searchCharacter', jsonParser, function (req, res) {
blizzard.wow.character(['profile', 'stats', 'items', 'statistics'], { origin: 'us', realm: req.body.realm.name, name: req.body.name })
.then(response => {
if(response.status != 200){
res.send("That character doesn't exist! Please enter a valid character name.");
} else {
console.log(response.data);
res.send(response.data);
}
});
});
//Second Post Request
app.post('/helloworld', jsonParser, function (req, res) {
console.log(req.body);
res.send("hello");
});
错误信息:
SyntaxError: Unexpected token #
at Object.parse (native)
at createStrictSyntaxError (c:\Users\RDubz\Documents\Interviews\EagleDream 12-7-17\Project\node_modules\body-parser\lib\types\json.js:157:10)
at parse (c:\Users\RDubz\Documents\Interviews\EagleDream 12-7-17\Project\node_modules\body-parser\lib\types\json.js:83:15)
at c:\Users\RDubz\Documents\Interviews\EagleDream 12-7-17\Project\node_modules\body-parser\lib\read.js:121:18
at invokeCallback (c:\Users\RDubz\Documents\Interviews\EagleDream 12-7-17\Project\node_modules\body-parser\node_modules\raw-body\index.js:224:16)
at done (c:\Users\RDubz\Documents\Interviews\EagleDream 12-7-17\Project\node_modules\body-parser\node_modules\raw-body\index.js:213:7)
at IncomingMessage.onEnd (c:\Users\RDubz\Documents\Interviews\EagleDream 12-7-17\Project\node_modules\body-parser\node_modules\raw-body\index.js:273:7)
at emitNone (events.js:67:13)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
Facepalm 我使用 var id = 0 并将其传递给我的函数,但没有意识到它需要作为对象或参数传递。感谢评论的人!
试试这个:
$scope.searchCharacter = function(){
var request = {name: $scope.charName, realm: $scope.selectedRealm};
//First post request
$http.post('/searchCharacter', request)
.then(function(response) {
//sets some variables
var id = 0;
//Second post request (append id to route).
$http.post('/helloworld/' + id)
.then(function(response) {
//sets some more variables
debugger;
});
});
}
//First post request
app.post('/searchCharacter', jsonParser, function (req, res) {
blizzard.wow.character(['profile', 'stats', 'items', 'statistics'], { origin: 'us', realm: req.body.realm.name, name: req.body.name })
.then(response => {
if(response.status != 200){
res.send("That character doesn't exist! Please enter a valid character name.");
} else {
console.log(response.data);
res.send(response.data);
}
});
});
//Second Post Request (get id from req.params.id)
app.post('/helloworld/:id', function (req, res) {
console.log(req.params.id);
res.send("hello");
});
它将 id
附加到 helloworld
请求,并使用 req.params.id
定义路由 helloworld/:id
以从请求中提取 ID。