Fetch() POST requests IV


#1

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.


#3

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.


#4

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.


#5

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.