javascript实现的ajax主要代码如下:
var xmlHttp = false; function ajaxScore(_ajaxCon){//ajax处理评分var callBackResult ;var _assetid = $('resource_id').innerHTML;if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, SafarixmlHttp = new XMLHttpRequest();}else {// code for IE6, IE5xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");}xmlHttp.setRequestHeader( "Connection", "close");var url="http://172.30.93.204:8078/gateway/assetScoreServlet?assetid="+_assetid+"&stbId=''&score="+_ajaxCon;//var url = "";//获取播放地址urlif (url) {xmlHttp.onreadystatechange = function(){if (xmlHttp.readyState == 4 ) {if(xmlHttp.status == 200){if(curTimeoutErrorInfo > 0)clearTimeout(curTimeoutErrorInfo) ;callBackResult = xmlHttp.responseText;//xmlHttp.responseText is callback value//$("film").innerHTML = callBackResult ;if(eval(callBackResult)[0].url == 'ok'){$("resultError").innerHTML = "评分成功!";$("error").style.visibility = "visible";setTimeout(function(){$("error").style.visibility = "hidden";},2000);}}}else{//network not successif(curTimeoutErrorInfo > 0)clearTimeout(curTimeoutErrorInfo) ;curTimeoutErrorInfo = setTimeout(function(){$("resultError").innerHTML = "评分失败,请检查网络连接!";$("error").style.visibility = "visible";setTimeout(function(){$("error").style.visibility = "hidden";},2000);},2000) ;}}xmlHttp.open("POST", url, true);xmlHttp.send();}else {//$("play_num_" + line).innerHTML = "";//URL not exist}//alert(_ajaxCon); }
下面也是一个简单的ajax写法:
var TSTimeSpan = 0;var requests = new Array();function ajax_create_request(context){ var pos = requests.length; if (pos < 10) //??? { for (var i = 0; i < pos; i++) { if (requests[i].readyState == 4) { requests[i].abort(); requests[i].context = context; return requests[i]; } } } else { for (var i = 0; i < pos; i++) { if (requests[i].readyState == 4) { requests[i].abort(); } requests[i].obj = null; requests[i]=null; } requests = new Array(); pos = 0; } requests[pos] = Object(); requests[pos].obj = XMLHttpRequest(); requests[pos].context = context; return requests[pos];}function ajax_request(userid, action, clientID, lastState, url, data, callback, httpresponse, context, objname){ var request = ajax_create_request(context); request.obj.onreadystatechange = function() { if (request.obj) { if(request.obj.readyState == 4) { httpresponse(new ajax_response(request)); if (callback) { callback(new ajax_response(request)); } } } } request.obj.open('GET', url, true); request.obj.setRequestHeader( "Accept", "text/xml"); request.obj.setRequestHeader("Content-Type", "text/xml"); request.obj.setRequestHeader("charset", "utf-8"); request.obj.setRequestHeader( "User-id", userid);//设置header request.obj.setRequestHeader( "User-action", action); request.obj.setRequestHeader( "TS-ClientID", clientID); request.obj.setRequestHeader( "X-Last-State", lastState); request.obj.setRequestHeader( "User-Json", data); request.obj.setRequestHeader( "Obj-Name", objname); request.obj.setRequestHeader( "S-Time", ""); request.obj.setRequestHeader( "TS-SpanTime", TSTimeSpan); request.obj.send(null);}function ajax_response(request){ this.request = request.obj; this.value = null; this.lastState = null; this.status = request.obj.status; this.context = request.context; this.value = this.request.responseText; this.clientid = this.request.getResponseHeader( "TS-ClientID"); this.sTime = this.request.getResponseHeader( "S-Time" );//获取header this.objName = this.request.getResponseHeader( "Obj-Name" ); TSTimeSpan = this.request.getResponseHeader( "TS-SpanTime" ); return this;}