Trouble following instructions for XHR Post Requests III


#1

XHR Post Requests III

I am having trouble following the instructions for the above exercise:

Inside the code block of shortenUrl() save this code to a const called urlWithKey:

url + ‘?key=’ + apiKey;

This code includes your API key at the end of the URL.

Here is the entire code; not all of it is relevant to this instruction (i.e., I dont know which block fo code I need to insert this instruction into)

// Include data for accessing Google APIs

const apiKey = 'I. AIzaSyB2sfWuybVQXFLw3x7YEq1HsIse6YR55Gw';
const url = 'https://www.googleapis.com/urlshortener/v1/url';

// Some page elements

const $inputField = $('#input');
const $expandButton = $('#expand');
const $shortenButton = $('#shorten');
const $responseField = $('#responseField');

// AJAX functions

function expandUrl() {
  const urlToExpand = url + 
        '?key=' + apiKey +
        '&shortUrl=' + $inputField.val();
  const xhr = new XMLHttpRequest(); 
  xhr.responseType = 'json';
  xhr.onreadystatechange = function() {
    if (xhr.readyState === XMLHttpRequest.DONE) {
      $responseField.append('<p>Your expanded url is: </p><p>' + xhr.response.longUrl + '</p>');
    }
  };
  
  xhr.open('GET', urlToExpand);
  xhr.send();
}

function shortenUrl() {
url + '?key=' + apiKey;
}

function expand() {
  $responseField.empty();
  expandUrl();
  return false;
}

function shorten() {
  $responseField.empty();
  shortenUrl();
  return false;
}

// Call functions on submit

$expandButton.click(expand);
$shortenButton.click(shorten);

#2

Any help will be greatly appreciated


#3

Instructions: Inside the code block of shortenUrl() save this code to a const called urlWithKey


#4

For some reason it worked now and didn’t work before.

Your code was technically wrong; this is what I had to put in the block:

const urlWithKey = url + '?key=' + apiKey

#5

Man, you need to focus. I just quoted your code.

Good that you found the answer by yourself. I’m just not sure this was worth a topic.

You could have just looked at expandUrl() btw.


#6

what do you mean? what do you mean?


#7

No, I ain’t doing this. Figure it out.


#8

I get it after looking around. Thanks for the input!