這篇文章主要介紹了基于apicloud中AJAX請求的示例分析,具有一定借鑒價值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
我們提供的服務有:成都網站制作、網站設計、微信公眾號開發、網站優化、網站認證、平遠ssl等。為上千企事業單位解決了網站和推廣的問題。提供周到的售前咨詢和貼心的售后服務,是有科學管理、有技術的平遠網站制作公司
get請求代碼:
api.ajax({ url:'http://m.weather.com.cn/data/101010100.html' //天氣預報網站的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', //該參數若不傳,則默認為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的文件系統中的任意文件。可設置多個文件,甚至是文件數組,如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請求-提交二進制流:
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的文件系統中的任意文件 } },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請求-顯示上傳進度:
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('進度:' + ret.progress); }else{ api.alert({msg:'上傳成功:\n' + JSON.stringify(ret)}); } }else{ api.alert({msg:JSON.stringify(err)}); } });
【端API使用api.ajax讀取接口數據】
<!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()">點我獲取數據</button> </body> <script type="text/javascript" src="../script/api.js"></script> <script> function showPersonInfo(){ api.showProgress();//顯示加載進度框 //使用api.ajax請求數據,具體使用方法和參數請看官方文檔,這里使用get方法演示 api.ajax({ url:'http://192.168.0.10/get.php',//如果地址訪問不到會請求出錯,請填寫自己的接口地址 method:'get', cache:'false', timeout:30, dataTpye:'json', },function(ret,err){ api.hideProgress();//隱藏加載進度框 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+'網絡狀態碼:'+err.statusCode)}); } }); } </script> </html>
感謝你能夠認真閱讀完這篇文章,希望小編分享的“基于apicloud中AJAX請求的示例分析”這篇文章對大家有幫助,同時也希望大家多多支持創新互聯,關注創新互聯行業資訊頻道,更多相關知識等著你來學習!
網頁名稱:基于apicloud中AJAX請求的示例分析
標題網址:http://m.newbst.com/article30/isgjpo.html
成都網站建設公司_創新互聯,為您提供軟件開發、網站制作、網站改版、網站維護、商城網站、網站收錄
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯