Fetch() POST requests IV

I’ve passed the instructions up to this point. Though when I press the shorten button, I do not get a response.!
Screenshot%20(2)|690x388
please see screen shot.

1 Like

Please remember to include a link to the exercise so we can work with the actual code. Post your raw code, as well as any error messages.

sorry, https://www.codecademy.com/courses/intermediate-javascript-requests/lessons/requests-ii/exercises/fetch-post-requests-iv?action=resume_content_item&course_redirect=introduction-to-javascript

// Information to reach API5b1f270479ab48a4bca52b064c691d3a
const apiKey = ‘<5b1f270479ab48a4bca52b064c691d3a>’;
const url = ‘https://api.rebrandly.com/v1/links’;

// Some page elements
const inputField = document.querySelector(’#input’);
const shortenButton = document.querySelector(’#shorten’);
const responseField = document.querySelector(’#responseField’);

// AJAX functions
const shortenUrl = () => {
const urlToShorten = inputField.value;
const data = JSON.stringify({destination: urlToShorten});
fetch(url,{
method:‘POST’,
headers: {
‘Content-type’: ‘application/json’,
‘apikey’: apiKey,
},
body: data
}).then(response => {
if (response.ok){
renderJsonResponse(response)
}
});
}

// Clear page and call AJAX functions
const displayShortUrl = (event) => {
event.preventDefault();
while(responseField.firstChild){
responseField.removeChild(responseField.firstChild)
}
shortenUrl();
}

shortenButton.addEventListener(‘click’, displayShortUrl);

I passed instruction 2. Yet, after I paste an url and press shorten nothing happens.

1 Like

Google has terminated support for the URL shortener so this API will no longer work.

Remove your credentials from the posted code but keep a record since those are Google related, not CC.

so I think I may have had a similar problem as the OP on this problem.

Where in step to it asked for renderJsonResponse(response) and when you click run it shows as wrong, and then you cant advance to step 3 and have to ask for the solution in order to get to the next lesson. See bug ticket #125521. I know my screenshot was wrong and that I didnt pass response in but after I corrected that it still wouldnt allow me to advance.