Using XMLHtmlRequest in SCORM

Using XMLHtmlRequest in SCORM

by Luis Miguel Gallego -
Number of replies: 5

Good morning, in my SCORM package there is a "main content page" in a file, which has the generic code for managing the loading and unloading (LMSInitialize, LMSFinalize, and stuff)

There are also "other content pages" in several files. The "main" simply loads the "other" via URL parameter. Each organization node of the SCORM module suplies this parameter, all of them are linked to the "main content page", the only difference is the parameter.

The thing begins here. I'm using a XMLHttpRequest object in my code to insert the body contents of the "other content pages" into the body of the "main content page" directly upon loading. All works fine outside, but when importing and loading the package in Moodle 1.9 latest stable release, it fails. After calling "send()" with the correct path, the returned "responseXML" is null.

I'd like to ask if It's a bug, or if it's some kind of blocking for security,... and if there is some way to solve. Please, even if there isn't, say me as soon as possible. Here is the code also, thanks.

function GetReferencedPageBodyContents(referencedPage)
{
    if (window.XMLHttpRequest)
    {
        // Código para los demás navegadores
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // Código para IE6 y IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    xmlhttp.open("GET", referencedPage, false);
    xmlhttp.send();
    xmlDoc = xmlhttp.responseXML; // <--This is null!!
    
    xmlBody = xmlDoc.getElementsByTagName("body")[0];
    return xmlBody.children;
}

Average of ratings: -
In reply to Luis Miguel Gallego

Re: Using XMLHtmlRequest in SCORM

by Andy Castles -

Hi Luis

I have limited experience in Moodle but from a JavaScript point of view, I would do two things to debug what's happening here.

1. Check that the "referencedPage" parameter does actually contain the correct URL to the page that you're trying to load. Maybe the URL parameter isn't getting passed into the page properly due to how Moodle can load pages via a file.php to check that a user is logged in to the site.

2. Use Firebug in Firefox to check what URL the XMLHttpRequest is actually trying to load and to see if the response is some kind of error or the page that you expected.

Hope that helps,

Andy

In reply to Andy Castles

Re: Using XMLHtmlRequest in SCORM

by Luis Miguel Gallego -

Hi Andy, thanks a lot for answering.

Well, the thing is that I actually used firebug to detect the problem exactly, but the URL is correct. The same exactly copy-pasted URL works externally, outside of the SCORM package. I think it shouldn't fail as it's a relative path, and file structure is the same both inside and outside the package.

The problem is after calling send(). The returned response is null inside the SCORM, and correct outside.

In reply to Luis Miguel Gallego

Re: Using XMLHtmlRequest in SCORM

by Andy Castles -

When the XHR object calls "send()", in the Net tab of Firebug can you see the request being made? Is the status "200 OK"?

If you click the "plus" symbol beside the request for that URL, check to see what the response tab shows, can you see the raw HTML of the page that you were trying to load?

Finally, and this is where my Moodle experience lacks, as the page is probably loaded via the "file.php" page that Moodle uses to restrict access of resources to authorised users, it's possible that Moodle is making some changes to the HTTP headers that are meaning that the XHR object isn't able to detect that the resource loaded successfully. Check that the resource shows the same HTTP headers in Firebug's "Headers" tab for when it's loaded via Moodle and without Moodle.

FWIW, we do most of our coding using the PrototypeJS library which has onFailure and onException events when the Ajax loading fails for one reason or another. JQuery has similar events. We find that these are more reliable and have better cross-browser support than using a simple "if (window.XMLHttpRequest)" and using the XHR object directly. You could try a sample page within Moodle using PrototypeJS or JQuery temporarily to see if they have the same issues and if they return any better error messages.

Hope that helps,

Andy

In reply to Andy Castles

Re: Using XMLHtmlRequest in SCORM

by Luis Miguel Gallego -

Well, the status is OK, and the raw HTML is being sended. I checked and both outside and inside the raw HTML is exactly the same, so why inside the SCORM, the same input isn't recognized as XML?? Maybe is what you say, Moodle is blocking headers or something in some way.

I hardly ever used both JQuery and PrototypeJS, but i'll try them. Thanks a lot for your help, i think you pointed me to the right path.

Luis