基于apicloudAJAX請(qǐng)求代碼合集
get請(qǐng)求代碼:
api.ajax({ url:'http://m.weather.com.cn/data/101010100.html' //天氣預(yù)報(bào)網(wǎng)站的WebService接口 },function(ret,err){ if (ret) { api.alert({msg:JSON.stringify(ret)}); } else { api.alert({msg:JSON.stringify(err)}); }; });
POST請(qǐng)求-Form表單提交:
api.ajax({ url: 'http://www.xxx.com/path/form', method: 'post', dataType: 'text', //該參數(shù)若不傳,則默認(rèn)為json data: { values:{name: 'devlp', password: '123456'} //鍵值對(duì) } },function(ret,err){ if (ret) { api.alert({msg:JSON.stringify(ret)}); } else { api.alert({msg:JSON.stringify(err)}); }; });
POST請(qǐng)求-單個(gè)/多個(gè)文件,文件組上傳:
api.ajax({ url: 'http://www.xxx.com/path/upLoad', method: 'post', data: { files:{myfile: 'filepath'} // filepath來(lái)自ios或者Android的文件系統(tǒng)中的任意文件??稍O(shè)置多個(gè)文件,甚至是文件數(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請(qǐng)求-提交二進(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請(qǐng)求-提交文件流:
api.ajax({ url: 'http://www.xxx.com/path/body', method: 'post', data: { stream:'filepath' // filepath來(lái)自ios或者Android的文件系統(tǒng)中的任意文件 } },function(ret,err){ if (ret) { api.alert({msg:JSON.stringify(ret)}); } else { api.alert({msg:JSON.stringify(err)}); }; });
POST請(qǐng)求-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請(qǐng)求-顯示上傳進(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請(qǐng)求數(shù)據(jù),具體使用方法和參數(shù)請(qǐng)看官方文檔,這里使用get方法演示 api.ajax({ url:'http://192.168.0.10/get.php',//如果地址訪問(wèn)不到會(huì)請(qǐng)求出錯(cuò),請(qǐng)?zhí)顚?xiě)自己的接口地址 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:('錯(cuò)誤碼:'+err.code+';錯(cuò)誤信息:'+err.msg+'網(wǎng)絡(luò)狀態(tài)碼:'+err.statusCode)}); } }); } </script> </html>
以上這篇基于apicloudAJAX請(qǐng)求代碼合集就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Ajax+smarty技術(shù)實(shí)現(xiàn)無(wú)刷新分頁(yè)
這篇文章主要介紹了Ajax+smarty技術(shù)實(shí)現(xiàn)無(wú)刷新分頁(yè)的相關(guān)資料,需要的朋友可以參考下2016-03-03AJAX亂碼與異步同步以及封裝jQuery庫(kù)實(shí)現(xiàn)步驟詳解
這篇文章主要介紹了異步通信技術(shù)AJAX亂碼問(wèn)題、異步與同步、手動(dòng)封裝一個(gè)jQuery庫(kù),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤如何解決
在django中,使用jquery ajax post數(shù)據(jù),會(huì)出現(xiàn)403的錯(cuò)誤,該如何解決呢?下面由腳本之家小編幫大家解決django使用ajax post數(shù)據(jù)出現(xiàn)403錯(cuò)誤,需要的朋友可以參考下2015-09-09jquery與php結(jié)合實(shí)現(xiàn)AJAX長(zhǎng)輪詢(xún)(LongPoll)
傳統(tǒng)的AJAX輪詢(xún)方式,客服端以用戶(hù)定義的時(shí)間間隔去服務(wù)器上查詢(xún)最新的數(shù)據(jù)。種這種拉取數(shù)據(jù)的方式需要很短的時(shí)間間隔才能保證數(shù)據(jù)的精確度,但太短的時(shí)間間隔客服端會(huì)對(duì)服務(wù)器在短時(shí)間內(nèi)發(fā)送出多個(gè)請(qǐng)求。2015-10-10滑輪滾動(dòng)到頁(yè)面底部ajax加載數(shù)據(jù)配合jsonp實(shí)現(xiàn)探討
滾動(dòng)下拉到頁(yè)面底部加載數(shù)據(jù)是很多瀑布流網(wǎng)站的做法,那來(lái)看看配合jsonp是如何實(shí)現(xiàn)的吧,小菜總結(jié)記錄之用特在此與大家一起分享,感興趣的朋友可以參考下哈2013-05-05ajax跨域請(qǐng)求js拒絕訪問(wèn)的解決方法
這篇文章主要介紹了ajax跨域請(qǐng)求js拒絕訪問(wèn)的解決方法,需要的朋友可以參考下2014-05-05ajax請(qǐng)求攜帶自定義請(qǐng)求頭header(跨域和同域)案例實(shí)戰(zhàn)教程
這篇文章主要介紹了ajax請(qǐng)求攜帶自定義請(qǐng)求頭header(跨域和同域)案例實(shí)戰(zhàn)教程,ajax請(qǐng)求是有同源策略的,雖然可以應(yīng)用CORS等手段來(lái)實(shí)現(xiàn)跨域,但是這并不是說(shuō)這樣就是“同源”了,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-10-10