- AJAX 异步get方法
- <script language="javascript">
- var photo_url='';
- var msg='';
- var error_flag= false;
- function p2p() {
- photo_url = "异步访问地址";
- ajaxUploadphoto(photo_url);
- }
- function showmsg()
- {
- if(error_flag)
- {
- msg = "上传成功,清登录" + msg + "查看";
- }
- else
- {
- msg = msg+"上传失败";
- }
- document.getElementById('msg').innerHTML= msg;
- }
- function ajaxUploadphoto(photourl)
- {
- var num = 0 ;
- var xmlhttp = "";
- try{
- xmlhttp = new ActiveXObject('Msxml2.XMLHTTP');
- }catch(e){
- try{
- xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
- }catch(e){
- try{
- xmlhttp = new XMLHttpRequest();
- }catch(e){
- }
- }
- }
- //"userNameCheck/checkUserName.jsp?userName="+document.form1.u_name.value
- xmlhttp.open("get",photourl,true);
- xmlhttp.onreadystatechange=function(){
- if(xmlhttp.readyState==4)
- {
- if( xmlhttp.status == 200)
- {
- var text = xmlhttp.responseText;
- var lenth= text.indexOf("success");
- if( lenth >=0 )
- {
- msg = text.substr(8);
- error_flag = true;
- }
- showmsg();
- }
- else
- {
- msg = "不能连接服务器,";
- showmsg();
- }
- }
- }
- xmlhttp.send(null);
- }
- </script>
- <div id="msg" οnclick="p2p()">TEST</div>
- AJAX同步 GET请求
- xmlHttp.open("get",url,false);
- xmlHttp.send(null);
- var text = eval(xmlHttp.responseText);
- =================================================================
- AJAX 同步 POST请求
- function getAjaxVerify(url,from)
- {
- var xmlHttp=null;
- try
- {
- // Firefox, Opera 8.0+, Safari
- xmlHttp=new XMLHttpRequest();
- }
- catch (e)
- {
- // Internet Explorer
- try
- {
- xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
- }
- catch (e)
- {
- xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
- }
- }
- xmlHttp.open("POST",url,false);
- var str = $(from).value;
- //str = "from="+str;
- str = "from="+encodeURIComponent(str);
- xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
- xmlHttp.send(str);
- return xmlHttp.responseText;
- // eval(xmlHttp.responseText);
- }
AJAX 标准使用
最新推荐文章于 2024-08-12 12:09:40 发布