Guide to making a reusable AJAX function in JavaScript

Currently, I'm working on developing a function that utilizes AJAX to call data from another server and then processes the returned data using a callback. My goal is to be able to make multiple calls to different URLs and use the distinct responses in separate functions.

However, when I make two calls, it only retrieves one dataset (battlefield). I am puzzled as to what I might be missing in this setup.

Interestingly, everything works perfectly fine if I make just one call (e.g., to Treehouse).


/* This function checks for AJAX availability and creates an AJAX request accordingly.
* Params: url - the API URL
*        type - the type of request (GET, POST) - default is GET
*        callback - function to process the AJAX response
*/
function makeRequest(url, type, callback) {
type = typeof type !== 'undefined' ? type : 'GET';
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
  httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
  try {
    httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
  } 
  catch (e) {
    try {
      httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
    } 
    catch (e) {}
  }
}

if (!httpRequest) {
  alert('Giving up :( Cannot create an XMLHTTP instance');
  return false;
}
httpRequest.onreadystatechange = function(){
  try {
    if (httpRequest.readyState === 4) {
      if (httpRequest.status === 200) {
        var response = JSON.parse(httpRequest.responseText);
        return callback(response);
      } else {
        alert('There was a problem with the request.');
      }
    }
  } catch(e) {
    alert('Caught Exception: ' + e.description);
  }
}
httpRequest.open(type, url);
httpRequest.send();
}

Here's how I am calling the function:


makeRequest('//teamtreehouse.com/davidendersby.json', 'GET', function(treehouseData){
  console.log(treehouseData);
  sortedTreehousePoints = sortObject(treehouseData.points, 'DESC');
  getTotalPoints(treehouseData);
  getPoints();
  getTreehouseBadges(treehouseData);
});

// Not Working:
makeRequest('http://api.bf4stats.com/api/playerInfo?plat=xone&name=davetherave2010&output=json','POST', function(battlefieldData){
  console.log(battlefieldData);
});

Answer №1

Seems like the variable `httpRequest` is currently declared in the global namespace. Remember to use var keyword for proper scoping. Here's an example:

function makeRequest(url, type, callback) {
type = typeof type !== 'undefined' ? type : 'GET';
var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
   httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
    ...

Answer №2

The only issue I can see is related to Best Practices, as well as receiving multiple responses. (On Line 67, there's a console.log(battlefieldData); and on Line 58, there's a console.log(treehouseData);)

I recommend checking your XAMPP, WAMP, Apache server or whatever you are using, and also trying out jQuery Ajax along with the shorthand methods $.get() and $.post()

EDIT There might be an issue with referential integrity due to both requests being ASYNC, but I'm not entirely certain about this.

EDIT 2 When mentioning referential integrity, I meant in the JavaScript environment rather than in the context of database referential integrity. The link provided may be misleading.

Answer №3

Your assistance has been greatly appreciated. The problem turned out to be a scoping issue, which was resolved by declaring the httpRequest variable inside the function.

While I have some familiarity with jquery, I am now expanding my expertise in pure javascript. I will be sure to explore strict mode further.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Express server experiencing a conflict with authentication sessions

Currently testing out a library found on this link and encountering a frustrating issue related to potential conflicts in cookie or header authentication. Upon logging into one account, everything runs smoothly. However, attempting to log into a different ...

Attempting to implement an EventListener to alter the navbar upon scrolling, unsuccessful at the moment

Exploring ways to update the navigation bar upon scrolling to shrink its size and modify the color scheme (specifically, transitioning from a transparent background to white and altering font colors). Below is the HTML snippet: /* Defining the overa ...

What is the best way to execute a function individually for every tag within a vue.js application?

I'm currently exploring the world of Vue JS and I thought it would be interesting to experiment with something new. Sometimes looking at the code first makes understanding it much easier than a lengthy explanation. Check out this code snippet on jsFi ...

The Electron BrowserWindow turns dark post execution of the .show() method

Revision: After some tinkering, I discovered that the issue was related to the order in which I created the windows. Previously, my code looked like this: app.whenReady().then(() => { createWindow(); spawnLoadingBlockWindow(); spawnGenerati ...

What is the best way to execute Jest tests concurrently using the VSCode extension?

Running the Jest tests in band is essential to prevent errors from occurring. However, I am unsure of how to resolve this issue. The tests run smoothly when executed through the command line. ...

How can I access the id_lang variable in React JS from outside its scope?

How do I access the 'id_lang' variable outside of the render function in order to pass it down for checking? const Navbar = () => { const getID = async (id) => { let id_lang = id; console.log(id_lang); } ret ...

What is the best way to share specific links on LinkedIn articles?

When the LinkedIn button is clicked, the entire link does not get passed when the image is clicked. However, the Facebook link works perfectly fine. The LinkedIn button used to work before, has anything changed since then? <div align="center"> < ...

Error encountered with AngularJS code when attempting to load content from another page using ajax

I'm currently tackling a challenge with AngularJs and php. Whenever I try to load content from another page, AngularJs seems to stop working. Let me provide you with a sample code snippet to illustrate my issue. main-page.php <div id="form-secti ...

What is the best way to obtain root access and utilize disk usage (du) within the main process of Electron?

In the process of developing a macOS application with the help of Electron, I encountered an issue. Attempting to execute the following command from the main process using ipcMain and NodeJS's exec: // Navigating to a directory and utilizing disk us ...

Displaying jQuery AJAX response through the use of a PHP script as a middle

After setting up an AJAX request that functions correctly, I encountered a problem with retrieving and displaying the data received from the request. Here's the script in question: var input = $("#input").val(); $(".pure-button").click(function() { ...

Error in TypeScript: Typography type does not accept 'string' type as valid

As I attempt to implement the Typography component from material-ui using TypeScript, I encounter a perplexing error message TypeScript is throwing an error: Type 'string' is not assignable to type 'ComponentClass<HTMLAttributes<HTMLE ...

I have come across this building error, what do you think is the cause of it?

My attempt to launch my company's React.js project, downloaded from Tortoise SVN, is encountering an issue. PS C:\Projects\Old EHR> yarn start yarn run v1.22.19 $ next start ready - started server on 0.0.0.0:3000, url: http://localhost:30 ...

choosing between different options within Angular reactive forms

I am attempting to create a select element with one option for each item in my classes array. Here is the TypeScript file: @Component({ selector: 'app-create-deck', templateUrl: './create-deck.component.html', styleUrls: [' ...

When an input element is being controlled from outside the modal using a portal, it is losing focus after a key press

[Resolved] The input component is experiencing a focus issue when any key is pressed, only when its value is controlled from outside of the portal. NOTE: I apologize for any confusion. While writing this post, I identified the problem in my code, but dec ...

Efficiently uploading images with AJAX when submitting a form

I am attempting to upload an image along with other variables in a form submission, and then execute my PHP code to store the image in the user's profile_picture table. I want the image upload to be integrated within the same form used for saving dat ...

Problem with vueJS List Transition not being triggered

Within my Vue JS App, I encountered a situation where I have a list of items that change order randomly when the user clicks a button. Despite successfully using Vue.set to dynamically reposition the list elements, I faced an issue with adding a transition ...

Discontinuing the mobx autorun feature upon componentWillUnmount

In my componentDidMount, I have the following autorun function: componentDidMount() { this.autoUpdate = autorun(() => { this.setState({ rows: generateRows(this.props.data) }) }) } Unfortunate ...

JavaScript: Different ways to utilize the reduce method

Creating an array for search purposes based on a string like BBC Sport, the desired output array will be: [ 'BBC', 'BB', 'B', 'Sport', 'Spor', 'Spo', 'Sp', 'S' ] An implement ...

Interactive tooltip feature in Apexchart

I need to include % in my Apexcharts tooltip after the Y value. Working with vue.js and without official documentation from apexchart, I managed to make it function correctly. This is what I have accomplished so far: data: function () { return { ...

Angular JS: Extracting the header from a CSV file

Just getting started with angular JS and I have a question. I need to take a CSV file from the user as input and then send it to the controller when they click submit. <button class="btn btn-primary" type="submit" ng-click="Input(File)">Submit</ ...