0

I am working on a website and I am trying to write a script which sends data once someone creates an account. But I get some errors(attached a screenshot) and can't find a way to fix them as I'm still an intern. Hopefully someone can help me.

Thank you.

if (val2 == 1) {
    fetch(hookURL, {
        method: 'GET',
        headers: {
            'Content-Type': 'application/json',
        }, 
        body: attrString,
        mode: 'no-cors'
    })
    .then((response) => {
        //console.log('hook returned, response= ', response);
  });

enter image description here

1
  • For better clarity in code, use a constant to explain what "1" is in val2 == 1. Also, refactor the object, so you can type something like fetch(hookURL, createContentObject('GET', attrString)). Otherwise, it will become spaghetti code in the end. Commented May 25, 2020 at 7:51

1 Answer 1

1

If You want to send data - use a different method ;)

Try POST instead of GET

Look at the below example from: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

const data = { username: 'example' };

fetch('https://example.com/profile', {
  method: 'POST', // or 'PUT'
  headers: {
    'Content-Type': 'application/json',
  },
  body: JSON.stringify(data),
})
.then(response => response.json())
.then(data => {
  console.log('Success:', data);
})
.catch((error) => {
  console.error('Error:', error);
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.