前段时间项目碰到一个这样的问题,利用URL来传递参数,如果参数里面包含了‘批’字,参数到后台后已,“批”字变为乱码。影响了数据的查询。一直解决不了这个问题,后来改用POST提交方式,把此问题规避了。现在把这段动态创建隐藏Form,然后自动提交的代码放在这里记录下:
//这个方法是自动创建一个Form表单,并指定表单的提交方式是post
window.getNewSubmitForm = function()
{
var submitForm = document.createElement("FORM");
document.body.appendChild(submitForm);
submitForm.method = "POST";
return submitForm;
}
//朝方法1创建的表单里面动态的创建元素
window.createNewFormElement = function(inputForm, elementName, elementValue)
{
var newElement = document.createElement("<input name='"+elementName+"' type='hidden'>");
inputForm.appendChild(newElement);
newElement.value = elementValue;
return newElement;
}
//准备好了,进行提交
window.openPostRequest= function(actionUrl,paras)
{
var submitForm = getNewSubmitForm();
for (i in paras) {
var _propertyName = i.toString();
var _propertyValue = paras[_propertyName];
createNewFormElement(submitForm, _propertyName, _propertyValue);
}
submitForm.action= actionUrl;
submitForm.submit();
}
调用方法如下:
1: 首先创建一个Object对象,把要创建的元素放到这个对象内
var object = new Object();
object.queryName = null;
object.updateFlag = null;
object.contentType=contentType;
object.keyWords=keyWords;
object.curStatus=curStatus;
object.spId=spId;
object.name=name;
object.cid=cid;
object.subPloyId=subPloyId;
object.singerName=singerName;
object.province=province;
object.pageSize=pageSize;
object.from=null;
object.to=null;
2:将元素传入
openPostRequest('/mdmc/spOutputOfficeData.cmd',object);