使用getSomethingWithAjax发送的数据有浏览器限制,我想知道如果我想发布一个大数据的话会替换getSomethingWithAjax()? 这是我的代码:此代码在textarea中打开一个文本文件,并允许用户输入或删除字符,在大约超过2800个字符时有限制。
<%
if(request.getParameter("thetext") != null) {
File savedHere = new File(request.getParameter("filepath"));
FileOutputStream out2 = new FileOutputStream(savedHere);
PrintStream p = new PrintStream(out2);
try {
boolean gotError = false;
if(request.getParameter("revert").equals("1")) {
System.out.println("In revert");
p.print(request.getParameter("thetextRev").replace("\r",""));
gotError = p.checkError();
p.close();
} else {
System.out.println("NOT in revert\n"+request.getParameter("thetext"));
p.print(request.getParameter("thetext").replace("\r",""));
gotError = p.checkError();
p.close();
}
if(request.getParameter("revert").equals("0")) {
if(!gotError) {
out.println("File was successfully modified");
} else {
out.println("Error while writing the file");
}
}
}
catch(Exception ex) {
System.out.println("Exception while trying to write out to file");
out.println("There was an error while writing file");
}
}
String filename = "";
java.lang.StringBuffer myStuff = new java.lang.StringBuffer(100000);
if(request.getParameter("STEP")!=null
&& request.getParameter("secret")!=null
&& !request.getParameter("secret").equals("yes")) {
filename = request.getParameter("autoFiles");
File chosenFile = new File(filename);
DataInputStream dis = new DataInputStream(new FileInputStream (chosenFile));
String data = null;
while((data = dis.readLine()) != null) {
myStuff.append(data+"\n");
}
//this is just to check capacity and current length of file
out.println("Capacity is:"+ myStuff.capacity()+ "characeter count is" +
myStuff.length());
%>
<br>
<form method="POST" class="example" action="/jsp/EditText.jsp" id=form2>
<input type=hidden name=filepath value="<%=filename%>">
<input type=hidden name=revert id=revert value="0">
<input type=hidden name=textarea value=true>
<br>
<center>
<textarea class="form-textarea resizable" id="textarea" style="width:90%;height:27em;
font-family:courier;font-size:14;"
name="thetext"><%=myStuff.toString()%></textarea>
<div style="display:none;">
<textarea id="textareaRev" style="width:90%;height:27em; font-family:courier;font-
size:14;"name="thetextRev"><%=myStuff.toString()%></textarea>
</div>
<br><br>
<input type=button name=submit value="Save Changes"
onClick="if(runOnSubmit(){
getSomethingWithAjax('EditText.jsp'+
getAllFormElementsAndMakeIntoURI(true),'','hereIsTheMainHeaderSpan',false,false);}">
<input type=button name=submitRev value="Cancel/Exit"
onClick="toggleTextRevert();
getSomethingWithAjax('EditText.jsp','','hereIsTheMainHeaderSpan')">
<%
} else {
//initial part here, not relevant
/*
this part sorts a list of files located in a specific directory in a
dropdown menu Auto files is the generated files from the dropdown menu
*/
}
%>
答案 0 :(得分:0)
假设您正在寻找一种独立于浏览器的Ajax方法,我已经找到了一些例子 时光倒流如下所示。
这对函数执行JavaScript同步发布请求的处理。关键在于 jsxml_open_request函数。它会查看浏览器的JavaScript支持中可用的内容 实例化请求对象。
未显示第三个函数,该函数为“parse_returned_xml”。它将处理XML消息 从网站返回并回复了已发布的请求。
// -----------------------------------------------------------------------------
// This function will handle the mechanics of posting a synchronous XMLHttpRequest
// and will return a result array. It uses a helper function "jsxml_open_request".
// Return array is:
//
// [ status, errMsg, xmlData ]
//
// If there is no error, 'errMsg' is null. Status values are:
//
// -1 = Failed early in XMLHttpRequest init
// 0 = Failed after XMLHttpRequest init
// 1 = Success, xmlData is populated
//
// -----------------------------------------------------------------------------
function jsxml_process_post_request(postVarList, postTargetPage)
{
var xmlParser = false;
var gobjDatabaseDom = null;
var xmlData = null;
var xmlParseStep = 0;
var requestObject = null;
var i = 0;
var postKey = '';
var postValue = '';
oRetArray = jsxml_open_request(postVarList, postTargetPage);
if (oRetArray[0] != 1) {
return( new Array(oRetArray[0], oRetArray[1], null) );
}
parameters = oRetArray[2];
requestObject = oRetArray[3];
requestObject.send(parameters);
if (requestObject.status != 200) {
return( new Array(0, requestObject.statusText, null) );
}
xmlRetArray = parse_returned_xml(postVarList, requestObject.responseText);
if (xmlRetArray[0] != 1) {
return( new Array(xmlRetArray[0], xmlRetArray[1], null) );
}
return( new Array(1, null, xmlRetArray[2]) );
}
function jsxml_open_request(postVarList, postTargetPage)
{
var requestObject = null;
var i = 0;
var postKey = '';
var postValue = '';
// Browser select code.
if(window.XMLHttpRequest) // branch for native XMLHttpRequest object
{
try {
requestObject = new XMLHttpRequest();
} catch(e) {
requestObject = null;
}
}
else if(window.ActiveXObject) // branch for IE/Windows ActiveX version
{
try {
requestObject = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try {
requestObject = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
requestObject = null;
}
}
}
if (requestObject == null) {
return( new Array(-1, 'Could not do new on XMLHttpRequest.', null, null) );
}
// Assemble the paramter list
parameters = '';
for(i = 0; i < postVarList.length; i++)
{
postKey = postVarList[i][0];
postValue = postVarList[i][1];
if (parameters.length == 0) {
parameters = postKey + '=' + postValue;
}
else {
parameters = parameters + '&' + postKey + '=' + postValue;
}
}
// Prepare the post request.
requestObject.open("POST", postTargetPage, false);
requestObject.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
requestObject.setRequestHeader('Accept', 'text/plain');
return( new Array(1, 'Success.', parameters, requestObject) );
}