Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.


Can't return Wiki API data

I wrote the following code, but it won't return the JSON data. The link to the code in Codepen is: https://codepen.io/mlestina/pen/OZpOQW?editors=1011

The error message using Control-Shift-J is: getRecipes: falling back to a synchronous message for: "https://s.codepen.io" LoginRecipes.jsm:244 inside function, searchTerm is: Stalin and x is: Stalin and wikiLink is: "https://en.wikipedia.org/w/api.php?action=opensearch&search=Stalin&format=json&origin=*" console_runner-ce3034e6bde3912cc25f83cccb7caa2b0f976196f2f2d52303a462c826d54a73.js:1 error occurred console_runner-ce3034e6bde3912cc25f83cccb7caa2b0f976196f2f2d52303a462c826d54a73.js:1 and x is: undefined console_runner-ce3034e6bde3912cc25f83cccb7caa2b0f976196f2f2d52303a462c826d54a73.js:1 POST XHR http://localhost:27275/command [HTTP/1.0 500 FAILED 15ms] POST XHR http://localhost:27275/command [HTTP/1.0 500 FAILED 0ms]

  The HTML is:

<form> <input type="text" name="a" id="a"><br>

<button onclick="passToJ()">Search</button> </form>

and the Javascript is: var x;

function passToJ() { var searchTerm = document.getElementById("a").value; x = searchTerm; var wikiLink = "https://en.wikipedia.org/w/api.php?action=opensearch&search=" + searchTerm + "&format=json&origin=*"; console.log("inside function, searchTerm is: ", searchTerm, " and x is: ", x, " and wikiLink is: ", wikiLink);

$.ajax({ url: wikiLink,

  dataType: "json",
  type: "GET",
  success: function(data) {
  error: function() {

console.log('error occurred'); },

}); };

console.log(" and x is: ", x);

2 Answers

Steven Parker
Steven Parker
216,017 Points

I added this to the top of the function and it seems to work consistently now:


You can also remove the form tags to eliminate the default behavior of forcing a refresh.

I removed the <form> tags from the HTML5 AND IT WORKED!!!!!!!! I was stuck for a couple of days on this one! Thank you!!!

So that others may understand why the coding works this way, I've added this explanation: The form tag makes the program and the browser believe that the form input is being sent to another website. But since no other website is specified in an action tag, the form tag causes the page to refresh itself, thereby preventing the input from being sent to the API. The event.preventDefault(); command in Javascript prevents this page refreshing, as does removal of the form tags.