Reading server files with XMLHttpRequest()

Have you ever needed to read in the contents of a file stored on your server?
Consider using XMLHttpRequest().
Advantages of PowerPac's XMLHttpRequest() over ToolBook's Http Post action:
http://www.pgsoftwaretools.com/powerpac/assessments/xmlhttprequest/index.html
Download tbk:
Using XMLHttpRequest() is a powerful way to add external file content to your applications on-the-fly!
Consider using XMLHttpRequest().
Advantages of PowerPac's XMLHttpRequest() over ToolBook's Http Post action:
- Will not lock up the browser waiting for a response. This is called Asynchronous communication and is the recommended way to make server requests to prevent your users from thinking the browser has locked up when it is simply waiting for the server to respond.
- Accepts BOTH get and post methods.
- Sends user events with response text when the function finishes.
- Simplest form of this function will simply read file contents and return it to an Action Editor variable.
The example below will read the "readme.txt" file that has been placed in the root folder of our exported content. - Asynchronous request with user event callback.
This example will make an asynchronous call and release control to the browser immediately. When the server responds, it will send a user event to the object indicated by name of self and place the file contents in the value parameter of the user event.
THEN, we add the rest of our actions to the user event for the object that is sent the user event.
- Cross-domain requests are prohibited (disallowed) by the browser security model.
- Your server may be configured to prohibit access to certain types of files. In this case rename your file to htm or some other permitted file type.
- XMLHttpRequest() does not generally work when testing content from the local hard drive. Upload the content to a file server or install XAMPP on your development machine. (However, Firefox can be configured to allow XMLHttpRequest() to work from files on the local hard drive.)
- Worse case scenario: use XMLHttpRequest() to post (method = "POST") to a php script that simply echoes the file contents back to the client browser. This method will circumvent all security issues as this will always be considered an accepted request method.
- Code: Select all
<?php
echo file_get_contents( [your path and filename] );
?>
http://www.pgsoftwaretools.com/powerpac/assessments/xmlhttprequest/index.html
Download tbk:
Using XMLHttpRequest() is a powerful way to add external file content to your applications on-the-fly!