了解juery----异步请求+渲染页面入门
  TEZNKK3IfmPf 2023年11月12日 31 0

从后端的角度去学习前端

发起请求:

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代码中引号外面就可以直接用了。
【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年11月12日 0

暂无评论

推荐阅读
  TEZNKK3IfmPf   2023年11月15日   31   0   0 jQuery
  TEZNKK3IfmPf   2023年11月15日   70   0   0 jQueryjs
  TEZNKK3IfmPf   2024年03月22日   71   0   0 jQueryeasyui
  TEZNKK3IfmPf   2024年03月29日   53   0   0 htmljQuery
  TEZNKK3IfmPf   2024年05月31日   32   0   0 jQuery选择器
TEZNKK3IfmPf