0

This code wont execute. I think there is something wrong with the xhttp.send() function. Because the alert function before it executes but the alert function after it doesn't execute:

<html>
<head>
    <title>PAGE OUTPUT</title>
    <script type="text/javascript">
    function aa()
    {   
        var xhttp;
        alert("hi welcome"); 
        if(window.XMLHttpRequest)
        {
            xhttp=new XMLHttpRequest();
            alert("hi");
        }
        else
        {
            xhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        xhttp.open("GET","vehicle.xml",false);
        alert("OPEN EXECUTED");
        xhttp.send();
        alert("SEND EXECUTED");
        xmlDoc=xhttp.responseXML;
        alert("HI I HAVE REACHED OVER HERE");
        var vehicle=xmlDoc.documentElement;
        var car=vehicle.firstChild.nodeValue;
        var price=car.firstChild.nodeValue;
        alert(price);
    }
    </script> 
</head>
<body>
<center><input type="submit" onclick="aa()"></center>
</body>

3
  • Are you trying this page as a file in local filesystem? That causes a cross-site XHR, and most browsers would forbid it. Commented Jan 19, 2013 at 5:57
  • both the file are in the same folder ...i dont think that should be a problem Commented Jan 19, 2013 at 6:18
  • browsers will fail to send any XHR if you visiting local files. See my answer below please. Commented Jan 19, 2013 at 9:21

2 Answers 2

1

You'd better to check if the URL is like file:///path/to/file the browser will fail to send any XHR from the page, for security reasons (nowadays that seems unreasonable).

You should launch a server for this case, and visit that page in http://localhost/path/to/file. The easiest way I know is to launch a Python SimpleHTTPServer. You might install python yourself if you are using MS Windows, or it will be available in Mac/any Linux distributions. Then follow these steps

  • launch a terminal (on MS Windows it's called a command line prompt, using Win+R then type cmd to launch one)
  • cd to the path where the files locate
  • execute python -m SimpleHTTPServer
  • visit http://localhost:8000/html_file in your browser
  • click on that button as usual

Now you will see those alerts. Additionally, you might have to change xmlDoc=xhttp.responseXML to xmlDoc = xhttp.responseText if necessary.

Then try jQuery, that's much easier for ajax.

Sign up to request clarification or add additional context in comments.

Comments

0

Cancel the submit button click

onclick="aa(); return false;"

1 Comment

i m trying to fetch some value from the xml file vehicle.xml but the statement xhttp.send() isnt executing i think ...both the file(ie vehicle.xml and this file are in the same folder )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.