Retrieve two distinct URLs using the XMLHttpRequest open method

Looking for assistance with the following code. Having an issue where only the last xmlhttp.open is being triggered, not both. Here's the code:

<script type="text/javascript">
function loadXMLDoc(obj){ 
  var xmlhttp; 
  var flag; 
  var mycode;
  mycode = obj;

  if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }else{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200){
    }
  }
   flag = document.getElementById(mycode);
   if (flag.value == "Uncall") {
     flag.value = "Call Patient";
     document.getElementById('myDiv'+mycode).innerHtml="<b><font color='#00FF00'>Called</font></b>";        
   }else{ 
     flag.value = "Uncall";
     document.getElementById('myDiv'+mycode).innerHtml="<b><font color='#00FF00'>Calling..</font></b>";
     **xmlhttp.open("GET","http://1.1.1.1/callnumber.cgi?"+mycode,true);
     xmlhttp.send();
     xmlhttp.open("GET","http://2.2.2.2/displaynumber.php?"+mycode,true);
     xmlhttp.send();**
   }
}
</script>

Any insights would be greatly appreciated.

Answer №1

Avoid recycling the identical request object.

The top recommendation is to refrain from creating this from scratch. Seek out a pre-existing library and leverage their features. JQuery is a solid choice, but there are also numerous alternatives available.

Answer №2

<script type="text/javascript>
function executeRequest(data){
  var xhr; 
  var flag;
  var code;
  code = data;
  flag = document.getElementById(code);
  if (flag.value != "Uncall"){
      makeRequest(data,"http://1.1.1.1/callnumber.cgi?");
      makeRequest(data,"http://2.2.2.2/displaynumber.php?");
      flag.value = "Uncall";
      document.getElementById('myDiv'+code).innerHTML="<b><font color='#00FF00'>Calling..</font></b>";
      } else { 
       flag.value = "Call Patient";
       document.getElementById('myDiv'+code).innerHTML="<b><font color='#00FF00'>Called</font></b>";
      }
}
function makeRequest(data,url){ 
  var xhr; 
  var code;
  code = data;

  if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
    xhr=new XMLHttpRequest();
  }else{// code for IE6, IE5
    xhr=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xhr.onreadystatechange=function(){
    if (xhr.readyState==4 & xhr.status==200){
    }
  }
   xhr.open("GET",url+code,true);
   xhr.send();
 }

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

The menu remains open at all times

Currently, I am developing a web-accessible menu that needs to comply with 508 standards. However, I encountered an issue where the menu does not close when I reach the last item using the TAB key on the keyboard. Additionally, I am looking for a solution ...

What is the method for arranging by oldest and newest based on date?

I need help organizing my presentation with a variety of books. How can I sort them from oldest to newest? const userBooks = [ { id: 1, name: "", author: "", genre: "", date: "2022-1-12", }, ...

Spotify API for Javascript (React) causing Axios 403 error: ERR_BAD_REQUEST

I've been working on a React app to retrieve the user's top tracks, but I've encountered an issue when making the request. The error message returned is: Request failed with status code 403 AxiosError: Request failed with status code 403 He ...

How to access and retrieve data from a USB flash drive using Javascript

I am looking to print PDF files from a USB flash drive. I have decided to use Mozilla Firefox and the R-kiosk plugin along with the open library PDF.js, but I am facing an issue. How can I read folders and files to create a tree structure without using ...

Removing children does not work properly; it only removes half of them

Why am I unable to remove all children if I delete half of them in this code? I keep getting an error saying child is not defined, even though I have declared it. Each node in my code has children spheres (nodes) and edges (lines), but when I try to dele ...

JSON file not found by the system

I am currently working on a basic program that consists of 3 files: 1. An HTML file named index.html 2. A JavaScript file named app.js 3. A JSON dataset called dataset.json I am struggling to make the browser recognize the data in my program. This is ...

Unable to adjust the width of a label element using CSS specifically in Safari browsers

Here's the issue I'm facing with my HTML code <input type="checkbox" asset="AAA" name="assets[AAA]" value="AAA" id="assets[AAA]" class="c_green" checked="checked"> <label for="assets[AAA]"></label> In my CSS, I have the follow ...

What is the quickest way to redirect a URL in WordPress?

Is it possible to redirect a URL instantly in WordPress using this script code? JavaScript Code: jQuery(document).ready(function() { var x = window.location.href; if (x == 'http://example.com/') { window.location.hr ...

What would be an effective method for sending a multitude of parameters to a controller?

I am currently working on an application that utilizes Java with the Spring framework and Javascript with AngularJs framework. The application features a table displaying a list of objects along with two text fields for filtering these objects. The filteri ...

Guide on using double quotation marks for keys and values in a JavaScript array

I have an array that contains buffer data shown below [{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> },{ Buffer_Data: <Buffer b5 eb 2d> }] I need to add double quotes to bot ...

What is the process for showcasing a local notification within my application?

Here is the code snippet I am working with: import { LocalNotifications } from '@ionic-native/local-notifications'; @Component({ selector: 'app-home', templateUrl: 'home.page.html', styleUrls: ['home.page.scs ...

Tips on setting dynamic headers in tabulator

Is there a way to dynamically set the header name passed from JSON? Additionally, how can I hide multiple columns in Tabulator instead of just one? I would also like to be able to show/hide multiple columns on button click. These questions pertain to a co ...

A specialized identifier for nested objects in a React component

I am currently working with a JSON data structure that looks like this: [ [ { city: x patients: x id: 1 }, { city: y patients: y id: 2 } ], [ { city: x patients: x id: 1 }, { city: y patients: y id: 2 } ...

Having trouble importing my function component into a router in reactjs. Need some guidance on how to properly set it up

I am working on a Slider component function called export default Slider. In my App.js file, I have the following code: function App() { return ( <Router> <Routes> <Route exact path='/' element={<Home />} /> ...

Enhance your website with a lightbox effect using FancyBox directly in your JavaScript code

I am currently experiencing an issue with implementing fancybox on my website. I have a website that features links to articles, which open in fancybox when clicked. The problem arises when a user tries to access an article directly. In this case, the use ...

Securing AJAX Requests: Encrypting GET and POST Requests in JavaScipt using Node.js

Looking for a way to secure ajax post and get requests using JavaScript. The process would involve: Server generates private and public key upon request Server sends the public key to client Client encrypts data with public key Server decrypts data wit ...

What are some ways to conceal the API connection within a button?

I have a code that allows me to perform a certain API call with a link. Here is an example: <a class="btn btn-default" href="https://testapi.internet.bs/Domain/Transfer/Initiate?ApiKey='.$user.'&Password='.$pass.'&Domain=&ap ...

Attempting to retrieve the value from a nested table within a table row using Jquery

<tr role="row" class="odd"> <td style="background-color:white"> <table> <tbody> <tr data-row-id="22"> <td id="pid"><input type="checkbox" class="sub_chk" value="8843" data-id="22">< ...

Integrate CSS and Javascript Plugins into your Ruby on Rails application

I am utilizing an HTML, CSS, and JS template to design the interface for my Rails application. Within this template, there are several plug-ins that are required in both CSS and JS formats. I have stored these plug-ins within the "/assets/plugins/" directo ...

Retrieving information from a nested array transferred to Javascipt / Jquery through JSON from PHP / Cakephp

Apologies for the basic question, but after hours of research, the examples I've found don't seem to fit the context of my array. I've successfully passed data from my CakePHP server to my jQuery JavaScript, but I'm struggling to make s ...