这可能是由于Heroku应用程序的路由配置问题引起的。请确保您的端点正确命名,并通过以下步骤解决问题:
确保您的端点具有正确的路径和名称,例如:
const endpoint = 'https://your-heroku-app.herokuapp.com/api/users';
该端点将与您的Heroku应用程序上的路由 "/api/users" 匹配。
确保您的应用程序具有正确的CORS(跨源资源共享)设置,以允许从Angular应用程序进行API调用。它可以在应用程序的服务器端设置中实现,例如:
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); next(); });
这将允许来自任何来源的前端请求与此应用程序进行通信。
确保您的端点使用正确的请求方法和请求体配置。例如:
this.http.post(endpoint, { username: 'testuser', password: 'testpassword' }).subscribe(...);
在这个例子中,我们使用POST方法将JSON对象作为请求体发送。
通过这些步骤,您的Angular应用程序应该能够成功与Heroku应用程序进行API调用。