0

I want to read a locally stored xml, and I want to read/write through javascript. my code is as follows

function saveBaseValue()
{
  var xmlhttp;
  var XMLname="file:///C:/Users/setting.xml"
  if (window.XMLHttpRequest)
    {
    xmlhttp=new window.XMLHttpRequest();
    xmlhttp.open("GET",XMLname,false);
    alert(xmlhttp.responseXML);
   }

}

In this case, xmlhttp.responseXML is null.

5
  • Use XMLHttpRequest .onload event handler to read results of asynchronous request. xmlhttp.onload = function() {alert(xmlhttp.responseXML)}; Commented Jul 10, 2016 at 4:07
  • 1
    Possible duplicate of xmlhttprequest for local files Commented Jul 10, 2016 at 4:13
  • guest271314 : what if I want a synchronous request? Commented Jul 10, 2016 at 4:24
  • this is a security issue, if any web can read your cookie file, so u can use <input type="file"> Commented Jul 10, 2016 at 4:31
  • @pracheese What are you trying to achieve? Commented Jul 10, 2016 at 5:02

1 Answer 1

4

you can achieve your goal by using the below code.

<!DOCTYPE html>
<html>
<head>
<script>
function saveBaseValue()
{
  var xmlhttp;
  var XMLname="file:///C:/Users/setting.xml"  
  if (window.XMLHttpRequest)
  {
   xmlhttp=new window.XMLHttpRequest();
   xmlhttp.onreadystatechange = function() 
   {
      if (xmlhttp.readyState == 4) 
      {    
         alert(xmlhttp.responseXML);
      }
   };
   xmlhttp.open("GET",XMLname,false);
   xmlhttp.send();
  }
 }
 saveBaseValue();
 </script>
 </head>
 <body>    
   <h1>Test</h1>
 </body>
</html>

By default your browser will not allow you to access this local file as this a security concern, however if you want to access it anyhow then close all chrome instance, go to command prompt, change the directory to where your chrome.exe is present and run below command.

.\chrome.exe --allow-file-access-from-files --disable-web-security
Sign up to request clarification or add additional context in comments.

1 Comment

Note, with the latest Chrome, I get the following warning: Web security may only be disabled if '--user-data-dir' is also specified with a non-default value.

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.