Reading server files with XMLHttpRequest()

Workarounds and usability notes.

Reading server files with XMLHttpRequest()

Postby Clifton » Sat Feb 22, 2014 8:32 am

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:
  • 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.
How to do it:
  1. 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.
    Image 2.png
    XMLHttpRequest with GET method
    Image 2.png (12.35 KiB) Viewed 603 times
  2. 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.
    Image 4.png
    XMLHttpRequest with GET and User callback
    Image 4.png (12.03 KiB) Viewed 603 times

    THEN, we add the rest of our actions to the user event for the object that is sent the user event.
    Image 5.png
    User event actions
    Image 5.png (1.35 KiB) Viewed 603 times
Limitations you may encounter:
  • 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] );
    ?>
    In the above example, the best approach would be to POST the filename as a name/value pair and let the php script fetch the file requested.
Example of what is described above:
http://www.pgsoftwaretools.com/powerpac/assessments/xmlhttprequest/index.html

Download tbk:
XMLHttpRequest.zip
ToolBook v9.01 example from link
(93.49 KiB) Downloaded 99 times

Using XMLHttpRequest() is a powerful way to add external file content to your applications on-the-fly!
Clifton
Site Admin
 
Posts: 732
Joined: Tue Jan 14, 2014 1:04 am

Return to General Discussion

Who is online

Users browsing this forum: No registered users and 3 guests

cron