Java Script cannot save XML data by invoking C# aspx page RRS feed

  • Question

  • Dear Expert,

    When using Java Script to invoke C# aspx web page(passing XML format value to it) for saving data to Access database, but failed, relevant code as follows:

    ----------------------------------Java Script(partial code)----------------------------------------------

    function saveTo()
     var xmlTemp = new ActiveXObject("Microsoft.XMLDOM");
     var root = xmlTemp.createNode(1,"data","");
     var node = xmlTemp.createNode(1,"row","");
     node.setAttribute("name", $("Value_LoginID").value);
     node.setAttribute("password", $("Value_Password").value);
     var action = "save.aspx";
     var postdata = xmlTemp.xml;

     AddButtonAction(action, postdata);

    function AddButtonAction(action, postdata)
     var _cfg = {name: "data", cgi: action, value: postdata};
     var sFormName = "_form_" + _cfg["name"] + "_";
     var sFrameName = "_frame_" + _cfg["name"] + "_";
     var oFormName = $(sFormName);
     var oFrameName = $(sFrameName);
     if(oFormName != null) {
     if(oFrameName != null) {
     if(window.Event) {
      var oFrame = document.createElement("iframe");
      oFrame.name = sFrameName;
     else {
      var oFrame = document.createElement("<iframe name='" + sFrameName + "'></iframe>");
     oFrame.id = sFrameName;
     oFrame.style.display = "none";
     var oForm = document.createElement("form");
     oForm.id = sFormName;
     oForm.name = sFormName;
     oForm.action = _cfg["cgi"];
     oForm.method = "post";
     oForm.style.display = "none";
     oForm.target = sFrameName;

     oInput = document.createElement("input");
     //oInput.type = "hidden";
     oInput.value = _cfg["value"];
     oInput.name = "post_data[]";


    function $(_sId)
     return document.getElementById(_sId);

    ----------------------------------Java Script(partial code)----------------------------------------------

    ----------------------------------C# code(save.aspx)----------------------------------------------

    private void Page_Load(object sender, EventArgs e)
      string post_data = Request.Form["post_data[]"];        
      XmlDocument root = new XmlDocument();
      XmlElement node = (XmlElement)root.DocumentElement.ChildNodes[0];
      string strsql = "Insert into TeamUser(fchrCodeID, fchrLoginID, fchrPassword) values('"+ md5(node.GetAttribute("name")) +"', '"+ node.GetAttribute("name") +"', '"+ md5(node.GetAttribute("password")) +"')";


      Response.Write("<script language=\"javascript\">;\r\n");

    ----------------------------------C# code(save.aspx)----------------------------------------------

    FYI. No syntax error(Java Script & C#) was found when debugging in VS2010, if the value of "postdata" is in normal string format(not XML format) then pass to save.aspx, the data can be saved successfully; But if passing the "postdata"(in XML format) to save.aspx, it will get into endless loop of one temporary JS file(generated by Debugger) when debugging in VS2010, and in IE end nothing is executed(this is a web application). Hope to get your kind and professional answer and solution very soon, thanks a lot!

    Wednesday, September 12, 2012 4:03 PM

All replies

  • Is there any expert who can solve this? Waiting ......

    Friday, September 14, 2012 2:22 PM
  • Don't know about expert, but I'm pretty sure the xml property is for nodes and not for xmldom objects so instead of

    var postdata = xmlTemp.xml;


    var postdata = xmlTemp.documentElement.childNodes.item(0).xml;
    Friday, September 14, 2012 11:38 PM
  • FYI. When using 

    var postdata = xmlTemp.xml;

    the value of postdata is "<data><row name="abc" password="123456"/></data>"

    and if change the code as metioned

    var postdata = xmlTemp.documentElement.childNodes.item(0).xml;

    the value of postdata is "<row name="abc" password="123456"/>"

    the problem remains, even if the break points are set in save.aspx, but the debugger will not go into the save.aspx when debugging.

    For your further assistance, thanks.

    Saturday, September 15, 2012 11:42 AM
  • FYI. if the "postdata" is in XML format, it seems the program doesn't move to save.aspx(though break points were set in it) when executing the"AddButtonAction" function, but if the "postdata" is in normal string format, everyone goes well.

    Saturday, September 15, 2012 2:00 PM
  • My mistake.  Where was the first break point set ?

    The problem may be the use of the loadXML method which is for loading an xml document in string form as in


    Please try using the load method instead which is for loading an xml document as in


    where I hope the use of a Request object as an argument will work.

    • Edited by mystifeid Sunday, September 16, 2012 2:03 AM
    Saturday, September 15, 2012 10:41 PM
  • the problem remains after using root.load(post_data);  
    Sunday, September 16, 2012 2:08 AM
  • What happens if instead of a form you use an http request so that you can set the content-type to text/xml.
    var oReq = new XMLHttpRequest();
    oReq.open("POST", sURL, false);
    oReq.setRequestHeader("Content-Type", "text/xml");
    Sunday, September 16, 2012 11:39 AM
  • Thanks Sir. Per further troubleshooting , the problem was caused by the value set for validateRequest(requestValidationMode/.Net 4.0), when it's set to "false", problem solved.
    Saturday, September 22, 2012 1:19 PM
  • I think it is becuase the validation expects text/plain.

    Came across your solution too and almost posted it except there are dire warnings about setting validateRequest to false.



    Did you try setting a custom content-type to text/xml as above? 

    Saturday, September 22, 2012 10:30 PM
  • Check if you have the XML well formed and it actually has a proper root node formed for the XML node.

    Piyush T

    Tuesday, January 8, 2013 6:18 AM