基于apicloudAJAX請求代碼合集
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: 'filepath1'}或者files:{'myfile[]': ['filepath', 'filepath1']}等 } },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>
以上這篇基于apicloudAJAX請求代碼合集就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Ajax+smarty技術(shù)實現(xiàn)無刷新分頁
這篇文章主要介紹了Ajax+smarty技術(shù)實現(xiàn)無刷新分頁的相關(guān)資料,需要的朋友可以參考下2016-03-03AJAX亂碼與異步同步以及封裝jQuery庫實現(xiàn)步驟詳解
這篇文章主要介紹了異步通信技術(shù)AJAX亂碼問題、異步與同步、手動封裝一個jQuery庫,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧2023-01-01django使用ajax post數(shù)據(jù)出現(xiàn)403錯誤如何解決
在django中,使用jquery ajax post數(shù)據(jù),會出現(xiàn)403的錯誤,該如何解決呢?下面由腳本之家小編幫大家解決django使用ajax post數(shù)據(jù)出現(xiàn)403錯誤,需要的朋友可以參考下2015-09-09jquery與php結(jié)合實現(xiàn)AJAX長輪詢(LongPoll)
傳統(tǒng)的AJAX輪詢方式,客服端以用戶定義的時間間隔去服務(wù)器上查詢最新的數(shù)據(jù)。種這種拉取數(shù)據(jù)的方式需要很短的時間間隔才能保證數(shù)據(jù)的精確度,但太短的時間間隔客服端會對服務(wù)器在短時間內(nèi)發(fā)送出多個請求。2015-10-10滑輪滾動到頁面底部ajax加載數(shù)據(jù)配合jsonp實現(xiàn)探討
滾動下拉到頁面底部加載數(shù)據(jù)是很多瀑布流網(wǎng)站的做法,那來看看配合jsonp是如何實現(xiàn)的吧,小菜總結(jié)記錄之用特在此與大家一起分享,感興趣的朋友可以參考下哈2013-05-05ajax請求攜帶自定義請求頭header(跨域和同域)案例實戰(zhàn)教程
這篇文章主要介紹了ajax請求攜帶自定義請求頭header(跨域和同域)案例實戰(zhàn)教程,ajax請求是有同源策略的,雖然可以應(yīng)用CORS等手段來實現(xiàn)跨域,但是這并不是說這樣就是“同源”了,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10