1. get方式
語(yǔ)法:
$.get(url, [data], [callback], [type])
參數(shù)說(shuō)明:
url 請(qǐng)求的地址
data 要發(fā)送給后臺(tái)的數(shù)據(jù): 要么是對(duì)象格式{key1:value1,key2:value2},要字符串拼接key1=value1&key2=value3
callback 回調(diào)函數(shù),返回服務(wù)器端的結(jié)果
type 返回的數(shù)據(jù)的格式,xml, json, text
1. get方式代碼
<script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
//1. get方式 $.get(url, [data], [callback], [type])
$.get("userInfo.json", function (userData) {
console.log("get方式", userData);
}, "json");
</script>
2. post方式
語(yǔ)法:
$.post(url, [data], [callback], [type])
參數(shù)說(shuō)明:
url 請(qǐng)求的地址
data 要發(fā)送給后臺(tái)的數(shù)據(jù): 要么是對(duì)象格式{key1:value1,key2:value2},要字符串拼接key1=value1&key2=value3
callback 回調(diào)函數(shù),返回服務(wù)器端的結(jié)果
type 返回的數(shù)據(jù)的格式,xml, json, text
2. post方式代碼
<script src="jquery-1.11.0.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript">
//2. post方式 $.post(url, [data], [callback], [type])
$.post("userInfo.json", function (userData) {
console.log("post方式", userData);
}, "json");
</script>
3. ajax方法,既可以支持get,也可以支持post
.ajax({
type: "POST", //發(fā)送的方式
url: "some.php", //請(qǐng)求的地址
data: "name=John&location=Boston", //要發(fā)送的數(shù)據(jù)
dataType: "返回的數(shù)據(jù)類型", //xml, json, text, jsonp【跨域請(qǐng)求】
success: function(msg){
//請(qǐng)求成功的回調(diào)函數(shù)
alert( "Data Saved: " + msg );
}
});
3. ajax方法代碼示例
<script src="jquery-1.11.0.js" type="text/javascript" charset="utf- 8"></script>
<script type="text/javascript">
//3. ajax通用方法
$.ajax({
type: "POST", //發(fā)送的方式
url: "userInfo.json", //請(qǐng)求的地址
data: "name=John&location=Boston", //要發(fā)送的數(shù)據(jù)
dataType: "json", //xml, json, text, jsonp【跨域請(qǐng)求】
success: function (userData) {
//請(qǐng)求成功的回調(diào)函數(shù)
console.log("ajax通用方法", userData);
}
});
</script>