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

JavaScript

do onload in javascript is method or property?

shalom to everybody. i am trying to make a "number facts" game and using a xhr.open. in the function i wrote: "<script> let fact = document.querySelector('#fact'); let facttext = document.querySelector('#fact-text'); let numberinput = document.querySelector('#number-input'); // הרצת פונקציה numberinput.addEventListener('input', getFactAjax); function getFactAjax(){ let number = numberinput.value; let xhr = new XMLHttpRequest(); xhr.open('GET', 'http://numbersapi.com/'+number); xhr.onload = function(){ if(this.status == 200 && number != ''){ console.log(this.responseText); } } xhr.send(); } </script>"

but it is not working, the console log dont replay. i think that the problem is in the xhr.onload

2 Answers

Steven Parker
Steven Parker
231,141 Points

This code seems OK, but the URL it is attempting to contact (numbersapi.com) does not seem to exist.

o.k i have a question about api and fetch. how do i get a good link for api-fetch. can i just use a regular link\domain or a special one?

Steven Parker
Steven Parker
231,141 Points

The API documentation from the service provide should include the specific URL(s) to use.