0

Hello i am new in XML HTTP request. and I am trying a code to load an XML file but didnt get the desired outcome.

code is : `<script> if (window.XMLHttpRequest) { var xhttp=new XMLHttpRequest(); } var url="../src/employee.xml"; xhttp.open('GET',url,true); xhttp.send(); xmlDoc=xhttp.responseXML; document.write("XML document loaded into an XML DOM Object."); </script> </body> </html>` 

employee is an xml file in the src folder which is <?xml version="1.0" encoding="utf-8"?> <employee> <branch="cse"> <name>Rahul</name> <age>21</age> </branch> </employee>
Thank You in advance.

3
  • did you try it without the ..? Commented Mar 21, 2013 at 20:22
  • yes i tried its not working. Commented Mar 21, 2013 at 20:27
  • after some testing i found send function is not working. Dont know what the error is but will be glad if someone tells me Commented Mar 21, 2013 at 22:12

1 Answer 1

1
var xmlhttp=new XMLHttpRequest(); xmlhttp.open("GET","/css/normalize.css?ThereIsNoSpring",false); xmlhttp.send(); 

I am thinking that the problem is that you are setting it to operate asynchronous, which would then require you to specify a callback.

open( Method, URL, Asynchronous, UserName, Password )

If the open method of the XMLHttpRequest object was invoked with the third parameter set to true for an asynchronous request, the onreadystatechange event listener will be automatically invoked for each of the following actions that change the readyState property of the XMLHttpRequest object.

Therefore, just set the third parameter to false, if you need it to be asynchronous, then set it to true and specify a callback like as follows:

 xmlhttp.onreadystatechange = function() { if (xmlhttp.readyState === 4){ alert(xmlhttp.responseXML); } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.