Undefined on HTTPRequest

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • computerfox
    Contributor
    • Mar 2010
    • 276

    #1

    Undefined on HTTPRequest

    Hello everyone,

    I have a small question. Here's my code:

    Code:
    <html>
    <head>
            <title>Test</title>
            <script>
                    function show(id){
                            var file="test.txt";
                            alert(file_get_contents(file));
                    }
                    function file_get_contents(path){
                            var httpRequest;
                            if (window.XMLHttpRequest) {
                                    httpRequest = new XMLHttpRequest();
                            }
                            else if (window.ActiveXObject) { // IE 8 and older
                                    httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
                            }
                            httpRequest.onreadystatechange = function(){
                                    if (httpRequest.readyState === 4) {
                                            if (httpRequest.status === 200) {
                                                    return httpRequest.responseText;
                                            }
                                    }
                            };
                            httpRequest.open('GET',path);
                            httpRequest.send();
                    }
            </script>
    
    </head>
    <body>
            <form enctype="multipart/form-data" >
                    <input type="file" width="100%" id="file"/><br/>
                    <button type="button" onclick="show('file')" width="100%" id="submit">Show</button>
            </form>
    </body>
    </html>
    For some reason I get undefined at the alert. Am I missing something?

    Thank you. FOX
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    That's because your file_get_conten ts function doesn't return anything. The function you assigned to the onreadystatecha nge event is returning data. But not the function that you tried to echo.

    Comment

    • computerfox
      Contributor
      • Mar 2010
      • 276

      #3
      I just noticed that.... Thanks Rabbit....

      Comment

      Working...