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', //該參數若不傳,則默認為jsondata: {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的文件系統中的任意文件??稍O置多個文件,甚至是文件數組,如files:{myfile: 'filepath', myfile1: 'filepath1'}或者files:{'myfile[]': ['filepath', 'filepath1']}等}},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>
以上這篇基于apicloudAJAX請求代碼合集就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持武林網。
新聞熱點
疑難解答
圖片精選