0

I am trying to make a Ajax get request to display a text file which is located in my public folder. The function is as follows;

var XMLHttpRequestObject = false;

if (window.XMLHttpRequest) {
    XMLHttpRequestObject = new XMLHttpRequest();

} else if (window.ActiveXObject) {
    XMLHttpRequestObject =
    new ActiveXObject("Microsoft.XMLHTTP");
}

function getData(datasource, divID) {
    if (XMLHttpRequestObject) {
        var obj = document.getElementById(divID);
        XMLHttpRequestObject.open("GET", datasource, true);
        XMLHttpRequestObject.onreadystatechange = function() {
            if (XMLHttpRequestObject.readyState == 4 
                && XMLHttpRequestObject.status == 200) {
                obj.innerHTML = XMLHttpRequestObject.responseText;
            }
        }

        XMLHttpRequestObject.send(null);
    } 
}

Calling the actual function then;

<form>
    <input type="button" value="Display Message" onclick="getData('AjaxGreek.txt', 'targetDiv')">
</form>

The error I am getting is "no route matches [get] "pages/AjaxGreek.txt"

Pages is the controller.

I understand the error in as much as the browser is looking for the page pages/AjaxGreek which doesn't exist, as it's the text file I am trying to display.

Any help would be much appreciated.

user2085143
  • 4,162
  • 7
  • 39
  • 68
  • And AjaxGreek.txt is located in `/public/AjaxGreek.txt`? – fivedigit Dec 14 '14 at 21:18
  • So this actually works If I just place it in the public folder. However, I would still like to have it work in a view with a controller. – user2085143 Dec 14 '14 at 21:19
  • fivedigit - yes it is! – user2085143 Dec 14 '14 at 21:20
  • I'd define a model that utilizes files for persistence, getting a classic MVC combination. Non-ActiveRecord models are quite often useful. Where do you want to store your files? Will you change them through the app or from the outside? – D-side Dec 14 '14 at 22:21
  • The application does not require a database. It's simply to demonstrate some client-side functionality for a small college project, however one of the requirements is to include a simple ajax request. So here, I simply want a text file to display when a user clicks a button! – user2085143 Dec 14 '14 at 22:29
  • To clarify, it's being displayed by having its contents appended to a div on the page. – user2085143 Dec 14 '14 at 22:31
  • Ok friends, found a solution but it's truly awful. in my routes I added get "/pages/AjaxGreek.txt" => redirect("/AjaxGreek.txt") My question now is how do i rewrite that line so I don't need to hardcode the data source in? – user2085143 Dec 15 '14 at 00:13

0 Answers0