了解juery----异步请求+渲染页面入门

了解juery—-异步请求

从后端的角度去学习前端

发起请求:

    function test(){
       $.ajax({
            //提交数据的类型 POST GET
            type:"POST",
            //提交的网址
            url:"testLogin.aspx",
            //提交的数据
            data:{Name:"sanmao",Password:"sanmaoword"},
            //返回数据的格式
            datatype: "html",//"xml", "html", "script", "json", "jsonp", "text".
            //在请求之前调用的函数
            beforeSend:function(){$("#msg").html("logining");},
            //成功返回之后调用的函数             
            success:function(data){
           $("#msg").html(decodeURI(data));            
            }   ,
            //调用执行后调用的函数
            complete: function(XMLHttpRequest, textStatus){
               alert(XMLHttpRequest.responseText);
               alert(textStatus);
                //HideLoading();
            },
            //调用出错执行的函数
            error: function(){
                //请求出错处理
            }         
         });

  }

根据name获取Input的内容:

$('input[name="text1"]').val()

然后是如何把多参数给传到data里面去:

var x = $("input[name='phone']").val();
data: {phone:x};

解析返回的数据:

                success:function (data) {
                    // alert(data);
                    var d = eval("(" + data + ")");
                    // alert(d);
                    var yzmcode = d.code;
                    if(yzmcode === 200){
                        alert("验证码发送成功");
                    }else{
                        alert("验证码发送失败");
                    }
                }

成功之后,跳转页面:

window.open //在另一个标签页里面打开
window.location.href //在当前页面打开

请求成功之后删除或添加某些东西

$("#dengluandzhuce").prepend("<a href=# class='dingbutuichu'>退出</a>");//添加
$("#dengluandzhuce").children().remove();//删除子元素

前端设置cookie


function getCookie(c_name)
{
    if (document.cookie.length>0)
    { 
        c_start=document.cookie.indexOf(c_name + "=")
        if (c_start!=-1)
        { 
            c_start=c_start + c_name.length+1 
            c_end=document.cookie.indexOf(";",c_start)
            if (c_end==-1) c_end=document.cookie.length
            return unescape(document.cookie.substring(c_start,c_end))
        } 
    }
    return ""
}

function setCookie(c_name,value,expiredays)
{
    var exdate=new Date()
    exdate.setDate(exdate.getDate()+expiredays)
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : "; expires="+exdate.toGMTString())
}

function checkCookie()
{
    username=getCookie('username')
    if (username!=null && username!="")
      {
          alert('Welcome again '+username+'!')
      }
    else 
      {
          username=prompt('Please enter your name:',"")
          if (username!=null && username!="")
            {
                setCookie('username',username,365)
            }
      }
}

去掉cookie:

setCookie("username","",-365);

将后端返回的信息,展示到网页中:

$("#mingziorphone").prepend("<strong>"+login+"</strong>");//只要在js代码中引号外面就可以直接用了。

猜你喜欢

转载自blog.csdn.net/xielinrui123/article/details/80534167