博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
javascript ajax
阅读量:5079 次
发布时间:2019-06-12

本文共 4005 字,大约阅读时间需要 13 分钟。

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;}

  

转载于:https://www.cnblogs.com/Mygirl/archive/2012/03/26/2418252.html

你可能感兴趣的文章
codevs 1080 线段树练习
查看>>
[No0000195]NoSQL还是SQL?这一篇讲清楚
查看>>
【深度学习】caffe 中的一些参数介绍
查看>>
Python-Web框架的本质
查看>>
QML学习笔记之一
查看>>
Window 的引导过程
查看>>
App右上角数字
查看>>
从.NET中委托写法的演变谈开去(上):委托与匿名方法
查看>>
小算法
查看>>
201521123024 《java程序设计》 第12周学习总结
查看>>
新作《ASP.NET MVC 5框架揭秘》正式出版
查看>>
IdentityServer4-用EF配置Client(一)
查看>>
WPF中实现多选ComboBox控件
查看>>
读构建之法第四章第十七章有感
查看>>
Windows Phone开发(4):框架和页 转:http://blog.csdn.net/tcjiaan/article/details/7263146
查看>>
Unity3D研究院之打开Activity与调用JAVA代码传递参数(十八)【转】
查看>>
python asyncio 异步实现mongodb数据转xls文件
查看>>
TestNG入门
查看>>
【ul开发攻略】HTML5/CSS3菜单代码 阴影+发光+圆角
查看>>
[ZJOI2007]棋盘制作 【最大同色矩形】
查看>>