一、ajax篇
起因:因项目需要,从来没有学过jquery、ajax的我,记录下了我的血泪史。。
1. 使用jquery,遇到 $ is not defined
可能的原因:
- 使用$的地方,在引入jquery之前
解决办法:
把jquery加载的代码写在最前面,如:
<script src="path/to/jquery"></script>
<script>
此处可以尽情使用$
</script>
- 使用iframe时,父页面加载了jquery,子页面使用$
解决办法:
在子页面中,写入:let $ = parent.$
然后,此时的$
已经是父页面的$
,之后使用父页面调用子页面的方法调用就行了。如:
$("#iframe的id").contents().find("#iframe中控件的id").事件(); // 方式1
$("#iframe中控件的id",document.frames("iframe的name").document).事情(); // 方式2
2. ajax死活获取不到后端返回的数据
产生的原因:
- 后端返回的数据类型,与ajax在
datatype
的类型不符
解决办法:
更改datatype
的值,为了更好理解,附上ajax的完整调用代码。
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(){
//请求出错处理
}
});
}