基于apicloud中AJAX請求的示例分析

這篇文章主要介紹了基于apicloud中AJAX請求的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。

我們提供的服務(wù)有:成都網(wǎng)站制作、網(wǎng)站設(shè)計、微信公眾號開發(fā)、網(wǎng)站優(yōu)化、網(wǎng)站認(rèn)證、平遠(yuǎn)ssl等。為上千企事業(yè)單位解決了網(wǎng)站和推廣的問題。提供周到的售前咨詢和貼心的售后服務(wù),是有科學(xué)管理、有技術(shù)的平遠(yuǎn)網(wǎng)站制作公司

get請求代碼:

api.ajax({
url:'http://m.weather.com.cn/data/101010100.html' //天氣預(yù)報網(wǎng)站的WebService接口
},function(ret,err){
if (ret) {
api.alert({msg:JSON.stringify(ret)});
} else {
api.alert({msg:JSON.stringify(err)});
};
});

POST請求-Form表單提交:

api.ajax({
url: 'http://www.xxx.com/path/form',
method: 'post',
dataType: 'text', //該參數(shù)若不傳,則默認(rèn)為json
data: {
values:{name: 'devlp', password: '123456'} //鍵值對
}
},function(ret,err){
if (ret) {
api.alert({msg:JSON.stringify(ret)});
} else {
api.alert({msg:JSON.stringify(err)});
};
});

POST請求-單個/多個文件,文件組上傳:

api.ajax({
url: 'http://www.xxx.com/path/upLoad',
method: 'post',
data: {
files:{myfile: 'filepath'}
// filepath來自ios或者Android的文件系統(tǒng)中的任意文件??稍O(shè)置多個文件,甚至是文件數(shù)組,如files:{myfile: 'filepath', myfile1: 'filepath2'}或者files:{'myfile[]': ['filepath', 'filepath2']}等
}
},function(ret,err){
if (ret) {
api.alert({msg:JSON.stringify(ret)});
} else {
api.alert({msg:JSON.stringify(err)});
};
});

POST請求-提交二進(jìn)制流:

api.ajax({
url: 'http://www.xxx.com/path/body',
method: 'post',
data: {
body:'textbits'
}
},function(ret,err){
if (ret) {
api.alert({msg:JSON.stringify(ret)});
} else {
api.alert({msg:JSON.stringify(err)});
};
});

POST請求-提交文件流:

api.ajax({
url: 'http://www.xxx.com/path/body',
method: 'post',
data: {
stream:'filepath'
// filepath來自ios或者Android的文件系統(tǒng)中的任意文件
}
},function(ret,err){
if (ret) {
api.alert({msg:JSON.stringify(ret)});
} else {
api.alert({msg:JSON.stringify(err)});
};
});

POST請求-Multipart-Data,文件和文本字段一起提交:

api.ajax({
url: 'http://www.xxx.com/path/multipart',
method: 'post',
data: {
values:{name: 'devlp', password: '123456'},
files:{file: 'fs://test.png'}
}
},function(ret,err){
if (ret) {
api.alert({msg:JSON.stringify(ret)});
} else {
api.alert({msg:JSON.stringify(err)});
};
});

POST請求-顯示上傳進(jìn)度:

api.ajax({
url: 'http://www.xxx.com/path/multipart',
method: 'post',
report: true,
data: {
values:{name: 'devlp', password: '123456'},
files:{file: 'fs://test.png'}
}
},function(ret,err){
if(ret){
if(0 == ret.status){
//loading('進(jìn)度:' + ret.progress);
}else{
api.alert({msg:'上傳成功:\n' + JSON.stringify(ret)});
}
}else{
api.alert({msg:JSON.stringify(err)});
}
});

【端API使用api.ajax讀取接口數(shù)據(jù)】

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/>
<title>test</title>
</head>
<body>
<button onclick="showPersonInfo()">點(diǎn)我獲取數(shù)據(jù)</button>
</body>
<script type="text/javascript" src="../script/api.js"></script>
<script>
function showPersonInfo(){
api.showProgress();//顯示加載進(jìn)度框
//使用api.ajax請求數(shù)據(jù),具體使用方法和參數(shù)請看官方文檔,這里使用get方法演示
api.ajax({
url:'http://192.168.0.10/get.php',//如果地址訪問不到會請求出錯,請?zhí)顚懽约旱慕涌诘刂?
method:'get',
cache:'false',
timeout:30,
dataTpye:'json',
},function(ret,err){
api.hideProgress();//隱藏加載進(jìn)度框
if(ret){
for(var i=0;i<ret.length;i++){
var html='<br>'+'ID:'+ret[i].id+'<br>'+'姓名:'+ret[i].name+'<br>'+'性別:'+ret[i].sex+'<br>'+'年齡'+ret[i].age; 
document.write(html);
}
}else{
api.alert({msg:('錯誤碼:'+err.code+';錯誤信息:'+err.msg+'網(wǎng)絡(luò)狀態(tài)碼:'+err.statusCode)});
}
});
}
</script>
</html>

感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“基于apicloud中AJAX請求的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創(chuàng)新互聯(lián),關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道,更多相關(guān)知識等著你來學(xué)習(xí)!

網(wǎng)頁名稱:基于apicloud中AJAX請求的示例分析
標(biāo)題網(wǎng)址:http://bm7419.com/article30/isgjpo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供軟件開發(fā)、網(wǎng)站制作、網(wǎng)站改版網(wǎng)站維護(hù)、商城網(wǎng)站、網(wǎng)站收錄

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)