在Angular中解决Stripe付款存在跨域问题,可以通过使用代理服务器来解决。下面是一个示例解决方法:
npm install http-proxy-middleware --save
const proxy = require('http-proxy-middleware');
module.exports = function(app) {
app.use(
'/api',
proxy({
target: 'https://api.stripe.com',
changeOrigin: true,
secure: false,
})
);
};
"scripts": {
"start": "ng serve --proxy-config proxy.conf.js"
}
https://api.stripe.com/v1/charges
,现在应该改为/api/v1/charges
。以Angular Service为例:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class StripeService {
private apiUrl = '/api/v1/charges'; // 使用代理服务器的URL
constructor(private http: HttpClient) { }
makePayment(paymentData: any) {
return this.http.post(this.apiUrl, paymentData);
}
}
现在,你的Stripe付款请求将通过代理服务器发送,解决了跨域问题。确保在开发环境中使用代理服务器,而在生产环境中使用正式的Stripe API URL。