Javascript to Post XML data to uri and get xml response


Javascript to Post XML data to uri and get xml response


Here is the html code to post xml data. Copy in notepad and save as html.

<html>  
 <head>  
 <title>WMS Test</title>  
 <script language="JavaScript">  
  function doit()  
  {  
   var url = 'http://10.1.253.119';  
   var method = 'POST';  
   var postData =  
    '<TRANS_MCU><!-- The root Is the transaction name. --><TRANS_COMMON_PARAMS> <MCU_TOKEN>0</MCU_TOKEN><MCU_USER_TOKEN>0</MCU_USER_TOKEN><MESSAGE_ID>1</MESSAGE_ID></TRANS_COMMON_PARAMS><!-- The next element Is the action (Login), And below it are the actions parameters, in this case, the Login parameters. --> <ACTION> <LOGIN><!-- The next parameters are the MCU IP And the port number. --><MCU_IP><IP>10.1.253.119</IP><LISTEN_PORT>80</LISTEN_PORT><HOST_NAME/></MCU_IP><!-- The user And password strings.--><USER_NAME>PMC</USER_NAME><PASSWORD>PMC</PASSWORD><STATION_NAME>EMA.F3-JUDITHS</STATION_NAME><COMPRESSION>false</COMPRESSION></LOGIN></ACTION></TRANS_MCU>';  
   var req = new XMLHttpRequest();  
   req.open("POST", url, true);  
   // req.setRequestHeader('User-Agent', 'XMLHTTP/1.0');  
   req.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');  
   req.onreadystatechange = function () {  
    if (req.readyState != 4) return;  
    if (req.status != 200 && req.status != 304) {  
     alert('HTTP error ' + req.status);  
     return;  
    }  
    alert(req.responseText);  
   }  
   if (req.readyState == 4) return;  
   req.send(postData);  
  }  
 </script>  
 </head>  
 <body>  
 <input type="button" value="doit" onclick="doit()"/>  
 <br/><br/><br/>  
 If you get error “No 'Access-Control-Allow-Origin' header is present on the requested resource” . you are doing an XMLHttpRequest to a different domain than your page is on. So the browser is blocking it as it usually allows a request in the same origin for security reasons. You need to do something different when you want to do a cross-domain request.  
 The easy way is to just add the extension in google chrome to allow access using CORS.  
 <br/><br/>  
 (https://chrome.google.com/webstore/detail/allow-control-allow-origi/nlfbmbojpeacfghkpbjhddihlkkiljbi?hl=en-US)  
 <br/><br/>  
 Just enable this extension whenever you want allow access to no 'access-control-allow-origin' header request.  
 <br/><br/>  
 Or  
 <br/><br/>  
 In Windows, paste this command in run window  
 <br/><br/>  
 <b>chrome.exe --user-data-dir="C:/Chrome dev session" --disable-web-security </b><br/><br/>  
 this will open a new chrome browser which allow access to no 'access-control-allow-origin' header request.  
 </body> 

Comments