mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
29 lines
725 B
JavaScript
29 lines
725 B
JavaScript
export default class FormSaver {
|
|
constructor(url, nonce) {
|
|
this.url = url;
|
|
this.nonce = nonce;
|
|
}
|
|
|
|
async save(form) {
|
|
const formData = new FormData(form);
|
|
const formJsonObj = Object.fromEntries(formData.entries());
|
|
|
|
const res = await fetch(this.url, {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
},
|
|
credentials: 'same-origin',
|
|
body: JSON.stringify({
|
|
nonce: this.nonce,
|
|
form: formJsonObj,
|
|
}),
|
|
});
|
|
|
|
const data = await res.json();
|
|
|
|
if (!data.success) {
|
|
throw Error(data.data.message);
|
|
}
|
|
}
|
|
}
|