LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-12-2018, 09:20 PM   #1
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Rep: Reputation: 15
AJAX xhrObj.onreadystatechange not working


I am trying to learn how to use AJAX to streamline an error-check process.

The webpage that will use this process asks a user to select a month and year from two separate drop-down (<select><option>) lists to view the newsletter published for that month-and-year. The YEARS list will include only and all years in which a newsletter was published, and the MONTHS list will include all twelve months. The error to be detected is the selection of a future month in the current year (e.g.: December 2018, selected in August of 2018).

Newsletters are uploaded to year-specific subdirectories and each issue is a PDF file named for the year and month of publication. For example, the April 2017 issue will saved as .../2017/2017-04.pdf. These year-directories are all subdirectories of the calling program's directory.

I had originally written a purely PHP error-checker, which works but it's inelegant and poses risk of trapping inexperienced users on a page they cannot use. I learned that AJAX could check for the existence of a URL without having to load a new webpage. The resource I found led me to develop the following code:
Code:
<script>
var xhrObj = false;
xhrObj = new XMLHttpRequest();

function postErrChk() {
  // Establish some variables.
  var monthVal = $("#postMonth").val();
  var yearVal = $("#postYear").val();

  // Check to see if the issue that has been uploaded.
  var issueToCheck =  "./" + yearVal + "/";
  issueToCheck += yearVal + "-" + monthVal + ".pdf";

  xhrObj.open("HEAD", issueToCheck);
  alert("You've requested " + issueToCheck);

    xhrObj.onreadystatechange = function() {
	alert("The readyState is " + xhrObj.readyState);
      if (xhrObj.readyState == 4) {
        if (xhrObj.status == 200) {
		  alert("The file has been found!");
        } else {
		  alert("The file cannot be found.");
        } // Close the if-else-statement testing status==200.
      } // Close the if-statement testing readyState==4.
    } // Close the anonymous function responding to onreadystatechange
}
</script>
When I put this in the webpage that also holds the <input> fields for the user's month-and-year selection, it works up to the point of
Code:
  alert("You've requested " + issueToCheck);
It accurately identifies the user's selection. However, none of the alerts following the
Quote:
xhrObj.onreadystatechange = function()
line pop up.

I have the impression that anonymous function() should reiterate until it reaches a (readyState == 4) condition, but this does not appear to be happening. In fact, it does not appear that the anonymous function() is firing at all.

I'm new to AJAX, and I've been trying everything I can think of to debug this, and I'm getting nowhere. Any clues?
 
Old 07-13-2018, 03:54 PM   #2
norobro
Member
 
Registered: Feb 2006
Distribution: Debian Sid
Posts: 792

Rep: Reputation: 331Reputation: 331Reputation: 331Reputation: 331
I don't know AJAX that well, but don't you need to send a request to the server?
Code:
 xhrObj.open("HEAD", issueToCheck);
 xhrObj.send(); // Try adding this line
 alert("You've requested " + issueToCheck);
 
Old 07-13-2018, 04:57 PM   #3
qajaq
Member
 
Registered: Sep 2008
Location: north-central Florida
Distribution: Lubuntu 19.04, Kubuntu 18.04
Posts: 120

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by norobro View Post
I don't know AJAX that well, but don't you need to send a request to the server?
Code:
 xhrObj.open("HEAD", issueToCheck);
 xhrObj.send(); // Try adding this line
 alert("You've requested " + issueToCheck);
norobro, thanks! That appears to do the trick. Seems the book I'm using has left out a pretty important bit.
 
  


Reply

Tags
ajax



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
To use or not to use Ajax clau_bolson Programming 2 04-08-2008 04:19 PM
LXer: Solid Ajax applications, Part 2: Building Ajax back ends LXer Syndicated Linux News 0 01-22-2008 12:30 PM
LXer: Working the “X” in Ajax LXer Syndicated Linux News 0 10-11-2006 01:24 AM
LXer: Eclipse Ajax Toolkit Framework and Ajax tools LXer Syndicated Linux News 0 05-12-2006 12:21 PM
LXer: Mastering Ajax, Part 3: Advanced requests and responses in Ajax LXer Syndicated Linux News 0 02-16-2006 06:46 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration