在Angular应用程序的生产环境中,CORS-anywhere可能不起作用,因为它是一个代理服务器,用于解决跨域资源共享(CORS)问题。在生产环境中,可能需要在服务器端配置CORS,而不是在客户端使用CORS-anywhere。
以下是一个解决方法的代码示例:
const express = require('express');
const app = express();
app.get('/api/data', (req, res) => {
res.setHeader('Access-Control-Allow-Origin', '*');
// 处理请求并返回数据
});
app.listen(3000, () => {
console.log('Server is running on port 3000');
});
import { HttpClient } from '@angular/common/http';
@Injectable()
export class DataService {
constructor(private http: HttpClient) { }
getData() {
const url = 'https://example.com/api/data'; // 替换为实际的服务器端点URL
return this.http.get(url);
}
}
请注意,以上示例代码只是一种解决方法的示例。具体的实现方式可能会因为服务器和Angular应用程序的配置而有所不同。请根据实际情况进行调整。