Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

APIs Posting Data with fetch()

Cant get a response to the console when I post using fetch()

As above, can someone check my code it is not working for some reason, thanks.

// ------------------------------------------ // EVENT LISTENERS // ------------------------------------------ select.addEventListener('change', fetchBreedImage); card.addEventListener('click', fetchBreedImage); form.addEventListener('submit', postData);

// --------------------

function postData(e) {s e.preventDefault(); const name = document.getElementById('name').value; const comment = document.getElementById('comment').value; fetch('https://jsonplaceholder.typicode.com/comments', { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, comment}) }) .then(checkStatus) .then(res => res.json()) .then(data => console.log(data))

}

2 Answers

Steven Parker
Steven Parker
231,184 Points

The unformatted code is a bit hard to read, and I don't have a context for testing it (could you post a link to a workspace snapshot next time?) But one thing caught my eye:

function postData(e) {
    s e.preventDefault();
//  ^
//  what's that letter "s" doing there?

Hello Steven, thanks removing the s sorted it out