Ajax-Post异步提交

本文代码转载自:http://vincent-feng.iteye.com/blog/342751
	var http_request = false;
var promptid = false;
function save(examPaperidDetailID,answerid,promptid_,submitButtonFlag,scoreid){
document.getElementById(promptid_).innerHTML = '保存中......'; //提示
promptid = promptid_;
var score = 0;
if (document.getElementById(scoreid) != null){
score = document.getElementById(scoreid).value
}
var factory = new XMLHttpRequestFactory();
http_request = factory.getHttpRequest3();
if (!http_request) {
alert('建议使用IE或Firefox浏览器!');
} else {
var linkurl = "/subsystem/talent/exampaper/SaveAnswerAjax.jsp"
var param = "id=" + Math.random()
+ "&examPaperidDetailID=" + examPaperidDetailID
+ "&answer=" + escape(document.getElementById(answerid).value)
+ "&submitButtonFlag="+submitButtonFlag
+ "&score="+escape(score);
//alert(param);
//将中文 放到 escape('中文') 否则IE提交的时候会丢失数据
// "id=" + Math.random() 这样子是为了每次都可以更新一下请求方式
http_request.open("POST", linkurl, true); // 利用POST 和 异步

// 下面这两句 比较重要,缺少会导致提交的数据为空
http_request.setRequestHeader("content-length",param.length); //post提交设置项
http_request.setRequestHeader("content-type","application/x-www-form-urlencoded"); //post提交设置项

http_request.onreadystatechange = callback;
http_request.send(param); // 参数在这里传进来
}
}

function callback() {
if (http_request.readyState == 4 && http_request.status == 200) {
if(document.getElementById(promptid) != null){
document.getElementById(promptid).innerHTML = http_request.responseText; //返回提示信息
}
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值