ITeye博客

Ajax-Post异步提交

qsrock · 2010-05-27 00:28 · ajax · Web前端 · Markdown

本文代码转载自: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)

暂无评论。

评论功能维护中,当前仅展示历史评论。