首頁 > web前端 > js教程 > 主體

基於apicloudAJAX請求代碼集合(絕對詳細)

亚连
發布: 2018-05-22 09:44:38
原創
1915 人瀏覽過

下面我就為大家帶來一篇基於apicloudAJAX請求程式碼集合。現在就分享給大家,也給大家做個參考。

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: '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:&#39;http://192.168.0.10/get.php&#39;,//如果地址访问不到会请求出错,请填写自己的接口地址
method:&#39;get&#39;,
cache:&#39;false&#39;,
timeout:30,
dataTpye:&#39;json&#39;,
},function(ret,err){
api.hideProgress();//隐藏加载进度框
if(ret){
for(var i=0;i<ret.length;i++){
var html=&#39;<br>&#39;+&#39;ID:&#39;+ret[i].id+&#39;<br>&#39;+&#39;姓名:&#39;+ret[i].name+&#39;<br>&#39;+&#39;性别:&#39;+ret[i].sex+&#39;<br>&#39;+&#39;年龄&#39;+ret[i].age; 
document.write(html);
}
}else{
api.alert({msg:(&#39;错误码:&#39;+err.code+&#39;;错误信息:&#39;+err.msg+&#39;网络状态码:&#39;+err.statusCode)});
}
});
}
</script>
</html>
登入後複製
上面是我整理給大家的,希望今後對大家有幫助。
相關文章:

如何利用jquery ajax實作檔案上傳功能

關於jQuery

ajax - ajax
() 的使用方法

#Yii upload實作AJAX上傳圖片的方法######### #############

以上是基於apicloudAJAX請求代碼集合(絕對詳細)的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!