XMLHttpRequest-使用POST重写XML文件 [英] XMLHttpRequest - using POST to rewrite XML file
问题描述
我设法学习并了解xmlhttp.open如何用于GET参数,但是我无法设法使POST参数起作用.
我在网页上有一个文本部分(从XML文件读取),下面有一个输入字段和一个按钮.
我想要当我按下该按钮时,文本部分更改为我在输入字段上写的内容. (该xml被重写并再次读取).我在重写文件时遇到问题.
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.
<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' />
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);
我应该如何处理信息到other.aspx上的xml以便更新xml文件?
How should I process the information to xml on other.aspx in order o update the xml file ?
Can you please correct me ?
推荐答案
您的代码中有几处错误.
1)xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
;这没问题,无需更改
2)在发布时,直接从Request对象而不是从查询字符串获取它
字符串xpto = Page.Request.QueryString["texto"];
..这是错误的.根据您other.aspx中的代码,它应该是字符串xpto = Request ["content"];
一旦您写入xml,然后读取它并使用response.write(the xml content)
.因此,您在此处编写的内容将通过响应文本收集.
3)此分配texto = xmlhttp.responseText;
是错误的.应该是texto.value = xmlhttp.responseText;
.
因此,这里是您的代码的更正版本,除了xml的读写部分..
在aspx ..
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
stringxpto = 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 useresponse.write(the xml content)
. So what you have written here will be collected through the response text.
3)this assignmenttexto = xmlhttp.responseText;
is wrong. It should betexto.value = xmlhttp.responseText;
.
So here is the corrected version of your code except the xml write and read part..
In the aspx..
<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>
在我的示例后面的Default2.aspx代码中..
in the Default2.aspx code behind in my example ..
protected void Page_Load(object sender, EventArgs e)
{
string content = Request["content"];
Response.Write("data received " + content);
}
通过按下按钮,您可以看到从default2.aspx收到的响应.我认为您可以处理xml读写部分.如有任何问题,请在此处发表评论.
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.
请检查"Content-Type".我认为它应该是文本/纯文本",因为它是一个txt文件.但是我还没有测试.
Please, Check "Content-Type". I think it should be "text/plain" since it is a txt file. but I have not tested.
这篇关于XMLHttpRequest-使用POST重写XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!