异步提交
function newXMLHttpRequest(){
var xmlreq = false;
if (window.XMLHttpRequest){
xmlreq = new XMLHttpRequest();
}else{
if (window.ActiveXObject){
try{
xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
}catch (e1){
try {
xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
}catch (e2){
xmlreq = false;
}
}
}
}
return xmlreq;
}
function getReadyStateHandler(req, responseXmlHandler) {
return function () {
if (req.readyState == 4) {
if (req.status == 200) {
responseXmlHandler(req.responseXML);
} else {
alert("HTTP error " + req.status + ": " + req.statusText);
}
}
};
}
String.prototype.trim = function(){
// 用正则表达式将前后空格
// 用空字符串替代。
return this.replace(/(^\s*)|(\s*$)/g, "");
};
function ajax(msgid){
req = newXMLHttpRequest();
var url="/personmanage/attention/zzy/myAttentionAction.do";
var paramString="m=update&msgid="+escape(msgid);
paramString=encodeURI(paramString);
//paramString=encodeURI(paramString); //有中文就要转两次
req.open("post", url, true);
req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
req.send(paramString);
req.onreadystatechange = callBack;
}
/**
* 发送广播完毕
*/
function callBack() {
if (req.readyState == 4){
if (req.status == 200){
var res=req.responseText;
if(res=="fail"){
jqueryDialog('您还未登录,请先登录。');
} else if(res=="true") {
jqueryDialog('更新成功!');
} else{
jqueryDialog( '处理失败,请稍后再试,或者联系运营部门......');
}
} else
jqueryDialog( '处理失败,请稍后再试,或者联系运营部门......');
}
window.location.reload();
}
java代码
PrintWriter pw = null;
try {
pw = response.getWriter();
pw.write(flag);
pw.flush();
} catch (IOException e) {
e.printStackTrace();
}finally{
pw.close();
}
return null;
jquery异步提交无刷新1、引用js文件:
<script type="text/javascript" src="/JavaScript/jquery-1.2.6.pack.js"></script>
2、编写js:
//业务种类改变
function sel_YWZL_Change(num)
{
var val = document.getElementById('GV_CommonData_ctl' + num + "_sel_YWZL").value;
var cx = document.getElementById('GV_CommonData_ctl' + num + "_sel_FYMC");
$.ajax({ type: "get", dataType: "html", url: "GetSFLC.aspx?YWZL="+encodeURI(val)+"×tamp=" + new Date().getTime(), data: "", success: function(data)
{
func_getCX(data,cx);
}});
}
//返回结果调用js
function func_getCX(fyxm, obj)
{
var arrsel = fyxm.split(',');
var alen = arrsel.length;
obj.options.length = 0;
var count = 0;
for(var i = 0; i < alen; i++)
{
obj.options.add(new Option(arrsel[i].toString(),arrsel[i].toString()));
if(obj.options[i].value != "运费")
count = count+1;
}
if(count == alen)
{
obj.options.add(new Option("运费","运费"));
}
obj.value = "运费";
}
3、新建异步调用页面:GetSFLC.aspx
if (Request["YWZL"] != null && Request["YWZL"].ToString() != string.Empty)
{
string ywzl = Request["YWZL"].ToString();
DataSet ds_fymc = new DataSet();
if (ywzl == "不限")
{
ds_fymc = _BasicDAL.GetProc("select FYXM from YWLXFYXMSZ where YWLX='运输'");
}
else
{
ds_fymc = _BasicDAL.GetProc("select FYXM from YWLXFYXMSZ where YWZL='" + ywzl + "' and YWLX='运输'");
}
if (ds_fymc != null && ds_fymc.Tables.Count > 0)
{
string[] DDParm = new string[] { "FYXM" };
DataTable dt = _gl.DefinitionTable(DDParm);
string fy = "";
for (int s = 0; s < ds_fymc.Tables[0].Rows.Count; s++)
{
fy += ds_fymc.Tables[0].Rows[s][0].ToString().TrimEnd(',') + ',';
}
string[] fyxms = fy.TrimEnd(',').Split(',');
for (int g = 0; g < fyxms.Length; g++)
{
DataRow dr = dt.NewRow();
dr["FYXM"] = fyxms[g].ToString();
dt.Rows.Add(dr);
}
DataTable dft = dt.DefaultView.ToTable(true, "FYXM");
string fym = "";
for (int x = 0; x < dft.Rows.Count; x++)
{
fym += dft.Rows[x][0].ToString() + ',';
}
Response.Write(fym.TrimEnd(','));
}
else
{
Response.Write("");
}
}
Response.End();