Was it just about the content-type?!

This commit is contained in:
2022-12-25 15:03:35 +01:00
parent ef2a17380b
commit ff9689f84c
2 changed files with 5 additions and 5 deletions

View File

@@ -33,7 +33,7 @@
const toggleFocus = () => inputInFocus = !inputInFocus const toggleFocus = () => inputInFocus = !inputInFocus
// Handle form submission // Handle form submission
async function handleForm (event: SubmitEvent) { async function handleForm (event: Event | HTMLFormElement) {
const data = new FormData(this) const data = new FormData(this)
const email = data.get('email') const email = data.get('email')
console.log('form:', data, email) console.log('form:', data, email)
@@ -41,9 +41,10 @@
if (email) { if (email) {
const req = await fetch(this.action, { const req = await fetch(this.action, {
method: 'POST', method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ email }) body: JSON.stringify({ email })
}) })
const result: FormStatus = await req.text() const result: FormStatus = await req.json()
formStatus = result formStatus = result
// If successful // If successful

View File

@@ -19,15 +19,14 @@ export const POST = (async ({ request, fetch }) => {
// Newsletter API request // Newsletter API request
const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${NEWSLETTER_LIST_ID}/contacts`, { const req = await fetch(`https://emailoctopus.com/api/1.6/lists/${NEWSLETTER_LIST_ID}/contacts`, {
method: 'POST', method: 'POST',
headers: { 'Content-Type': 'application/json' }, headers: { 'content-type': 'application/json' },
body: JSON.stringify({ body: JSON.stringify({
api_key: NEWSLETTER_API_TOKEN, api_key: NEWSLETTER_API_TOKEN,
email_address: email, email_address: email,
}) })
}) })
const res = await req.text() const res = await req.json()
console.log('server:', res) console.log('server:', res)
return new Response(res)
// Other error // Other error
if (res && res.status !== 'PENDING') { if (res && res.status !== 'PENDING') {