Click here to Skip to main content
15,888,182 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
I managed to learn and understand how xmlhttp.open works for GET parameter, however I cant manage to make POST parameter works.

I have a text section (reading from XML file) on a webpage, and I have a input field below and a button.

I want when I press that button the text section changes to what I wrote on input field. (the xml is rewritten and read again). I'm having problems what the rewrite of the file.

XML
<div id="myDiv"><h2>Here is the content read from XML File</h2></div>
   <input type='text' id='texto' /><br />
   <input type='button' onclick='alterar()' value='testar aqui' />


C#
function alterar() {
            var xmlhttp;
            var texto = document.getElementById('texto');
            alert(texto.value);

            if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
                xmlhttp = new XMLHttpRequest();
            }
            else {// code for IE6, IE5
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            xmlhttp.onreadystatechange = function () {

                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                    texto = xmlhttp.responseText;
                    alert("success");
                }

                else {
                   // alert("reach here");
                    alert(xmlhttp.readyState + " " + xmlhttp.status);
                }

            }

var data = "file=info.txt"  "&content=" + texto.value;
            xmlhttp.open("POST", "other.aspx", true);
            xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
            xmlhttp.send(data);





How should I process the information to xml on other.aspx in order o update the xml file ?

Can you please correct me ?
Posted
Updated 27-Feb-11 18:00pm
v7
Comments
Albin Abel 27-Feb-11 23:08pm    
My dear, there is no word in english like 'readed' or writed :) . You havn't explained the question fully. What you are entering in the text box. The text section name in the xml?. Then what you want to write in the XML?. Please make it clear.
[no name] 28-Feb-11 0:01am    
Edited for correcting spelling of rewriten and readed....
Maxdd 7 28-Feb-11 6:20am    
Thanks for the correction :)

Hi A few things wrong in your code.

1) xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");this is no problem, no need to change

2) When you are posting directly get it from the Request object, not from query string
string xpto = Page.Request.QueryString["texto"]; ..this is wrong. According to your code in the other.aspx it should be string xpto=Request["content"];

once you written to the xml then read it and use response.write(the xml content). So what you have written here will be collected through the response text.

3)this assignment texto = xmlhttp.responseText; is wrong. It should be texto.value = xmlhttp.responseText; .

So here is the corrected version of your code except the xml write and read part..

In the aspx..

XML
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script type="text/javascript">
    function alterar() {
        var texto = document.getElementById('texto');
        alert(texto.value);
        var xmlhttp;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        }
        else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                texto.value = xmlhttp.responseText;
                alert("success");
            }
            else {
                // alert("reach here");
                alert(xmlhttp.readyState + " " + xmlhttp.status);
            }
        }
        var data = "file=info.txt&content=" + texto.value;
        xmlhttp.open("POST", "Default2.aspx", true);
        xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlhttp.send(data);

    }
</script>
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
   <div id="myDiv"><h2>Here is the content readed from XML File</h2></div>
   <input type='text' id='texto' /><br />
   <input type='button' onclick='alterar()' value='testar aqui' />
</asp:Content>


in the Default2.aspx code behind in my example ..

C#
protected void Page_Load(object sender, EventArgs e)
{
    string content = Request["content"];
    Response.Write("data received " + content);
}




By pressing button you can see the response received from the default2.aspx. I think you can handle the xml reading and writing part. If any questions post comment here.
 
Share this answer
 
Comments
Maxdd 7 28-Feb-11 6:49am    
Thank you very much for helping.

I've deleted the asp content control because I was getting error's with master page on information so I suppose that's because I'm not using .NET 4.0. It works :)

A question: another solution was using Page Methods:

Making an ajax call, PageMethods.writeFile(...) and then on writeFile function do the writing I made of "Default2.aspx".

In your opinion, what should be better?
Sandeep Mewara 28-Feb-11 11:12am    
Good answer!! 5+++
Maxdd 7 1-Mar-11 9:26am    
Anyway if anyone can reply about my previous question I appreciate :)
Please, Check "Content-Type". I think it should be "text/plain" since it is a txt file. but I have not tested.
 
Share this answer
 
Comments
Maxdd 7 27-Feb-11 18:25pm    
Okay, but how should I process the data to write to XML ?
Maxdd 7 27-Feb-11 21:57pm    
I placed:

var content = "xpto";
var url = "info.xml";
var data = "file=" + url + "&content=" + content;
// var data = "dados";
xmlhttp.open("POST", "test.aspx", true);
//xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.setRequestHeader("Content-Type", "text/plain");
xmlhttp.send(data);

and then on test.aspx.cs page load

string xpto = Page.Request.QueryString["texto"];

// Create a new file in C:\\ dir
XmlTextWriter textWriter = new XmlTextWriter("d:\\myXmFile.xml", null);
// Opens the document
textWriter.WriteStartDocument();
// Write comments
textWriter.WriteComment(xpto);

....

However still cant write what I input to XML :(
Member 10684892 30-Jan-15 7:44am    
nnfngnfgnfn

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900