【问题标题】:angular 2 http get request not fetching data via node server while http post works fineangular 2 http get 请求未通过节点服务器获取数据,而 http post 工作正常
【发布时间】:2025-12-05 11:05:02
【问题描述】:

我正在尝试使用 angular2 和节点服务器从 Mongo DB 获取数据 通过 http 获取请求。当我试图查看获取响应时 浏览器,我可以将 html 标签视为“_body”的值,但如果相同的 http get 请求更改为http post然后响应如下:

"{"success":true,"sellerdata":[]}" as value of "_body".

为什么 post 使用相同的代码配置但没有收到请求?

这是我的角度代码。所有必需的导入都存在于类中。
这是创建 observable 的服务类。

/**(post changes which is working fine)return 
this.http.post('/sellerlist',{}).map(**/

@Injectable()
export class AdminService {
  constructor(private http:Http) { }
  getSellerdata(){
    return this.http.get('/sellerlist').map( 
      (res:Response ) =>{return res.json();})      
  }}

这是发起请求并在浏览器控制台上记录响应的订阅者类。

@Component({selector: 'app-admin-sellers',})
export class AdminSellersComponent implements OnInit {
  constructor(private adminService :AdminService) { }
  ngOnInit() {
    this.adminService.getSellerdata().subscribe(
      (dataFromServer) => {console.log(dataFromServer);});  // 
  }} 

这是我的节点服务器 JS。所有服务器端配置都在这里。

const express = require('express');
const path = require('path');
const http = require('http');
const bodyParser = require('body-parser');
const jwt = require('jsonwebtoken');
const mongoose = require('mongoose');
const app = express();
const port = process.env.PORT || '3000';
app.set('port', port);
const server = http.createServer(app);
mongoose.Promise = require('bluebird');
mongoose.connect('localhost/testapp')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static(path.join(__dirname, 'dist')));
app.get('*', (req, res) => {
  console.log(req.body);
  res.sendFile(path.join(__dirname, 'dist/index.html'));
});
app.use('/secure/*', secureRoutes);
function secureRoutes(req, res, next) {
  var token = req.headers['token'];
  if (token) {
    jwt.verify(token, process.env.SECRET_KEY, function (err, decode) {
      if (err) {
        res.status(500).send('Email & Password do not match');
      } else {
        next();
      }});
  } else {console.log(2)}};
app.all("/secure/*", secureRoutes, function (req, res, next) {
  next();});
app.get('/sellerlist', getSellerList);
/**(post changes which is working fine)
app.post('/sellerlist', getSellerList);**/

【问题讨论】:

  • 可能是因为发送POST请求时服务器没有响应数据。
  • 不清楚你在这里问什么。你是说 1. 响应中没有返回预期的数据 2. 不知道如何处理响应中的数据。我所看到的只是一个.subscribe(),里面有一个console.log(),并且没有尝试在任何地方实际分配该值。如果 1 是问题所在,那么您的问题缺少相关代码或对实际使用数据库本身的函数的测试。当然,这只是一个 GET 方法。这里没有帖子。
  • 你的节点js中没有方法app.post('/sellerlist')
  • @NeilLunn 是的,我正在尝试使用 console.log 查看预期数据,一旦我看到数据,我会将其分配给相关代码。我的问题是如果我替换 this.http。 get('/sellerlist') 与服务类中的 this.http.post('/sellerlist',{}) 和 app.get('/sellerlist', getSellerList); with app.post('/sellerlist', getSellerList); ,它在浏览器控制台中正确打印数据。希望现在我的问题很清楚
  • @ParthGhiya 由于帖子正在运行,我上面没有提到,但我想了解为什么 get 不能与节点服务器一起使用。仅供参考。如果我在服务类和 app.get('/sellerlist', getSellerList) 中将 this.http.get('/sellerlist') 替换为 this.http.post('/sellerlist',{}); with app.post('/sellerlist', getSellerList); ,它在浏览器控制台中正确打印数据

标签: node.js mongodb angular


【解决方案1】:
Problem was present in server side configuration.

app.get('*', (req, res) => {
  res.sendFile(path.join(__dirname, 'dist/index.html'));
}); 

is written before

app.get('/sellerlist', getSellerList);

So all get requests are handled by the default configuration . After reversing the above order , get requests worked.

【讨论】: