首先,我们需要确保我们使用的是正确的 URL。在这种情况下,您可以在http.post()
请求中指定完整的 URL,或者使用相对于应用程序的根路径的相对 URL。
例如,这个例子将使用完整的 URL:
this.http.post('https://example.com/api/test', { data: 'test' })
.subscribe(response => {
console.log(response);
});
或者,使用相对 URL,如下所示:
this.http.post('/api/test', { data: 'test' })
.subscribe(response => {
console.log(response);
});
如果您仍然在遇到重定向问题,您可以考虑使用 HttpClient
中的 withCredentials
选项。启用 withCredentials
选项将启用跨域请求时发送凭据,例如 Cookie 或授权标头。
例如:
this.http.post('/api/test', { data: 'test' }, { withCredentials: true })
.subscribe(response => {
console.log(response);
});
这个问题可能也可能不是由于 URL 的原因。如果您仍然遇到重定向问题,请提供更多的详细信息,以便我们可以帮助您更好地解决它。