0

I want to test php-Ajax with a simple program.I am using html and php file both are stored in same directory (/var/www/html). when I click on button it shows following error in console. XML Parsing Error: no root element found Location. ??

html file

<html>
    <head>
        <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
        <meta content="utf-8" http-equiv="encoding">
        <title> Ajax testing </title>
        <script type="text/javascript">
            function load(){
                    var xmlhttp = new XMLHttpRequest();
                    xmlhttp.onreadystatechange = function() {
                    if (this.readyState == 4 && this.status == 200) {
                        document.getElementById("result").innerHTML = this.responseText;
                    }
                };
                xmlhttp.open("GET", "simple.php" , true);
                xmlhttp.send();
            }
        </script>
    </head>
    <body>
        <button onclick="load();">Click here</button>
        <div id="result"> </div>
    </body>
</html>

php file

<?php 
    echo "Hello";
?>

what is wrong with this code ?

Preetam
  • 31
  • 1
  • 7

1 Answers1

0

This thread explains it xmlhttprequest for local files

From the thread:

Historically, you can't query for local files from JavaScript (or shouldn't be allowed to, or something's odd). This would be a serious breach of security.

There are only a few circumstances where you can do this, but in general they involve specific security settings requiring to be set for your browser, to either lift the limitation or to notify the current page's execution process that that is is granted this exceptional right. This is for instance doable in Firefox by editing the properties. It's also commonly OK when developing browser extensions (for instance for Chrome or FF) if they request the file access permissions.

I just installed Apache server and saved my php-file on that. I know it is a bit of an overkill, but hey, it worked :)

Community
  • 1
  • 1
numbers3567
  • 161
  • 1
  • 3
  • 8