>  기사  >  웹 프론트엔드  >  apicloud는 AJAX 요청을 구현합니다(코드 포함).

apicloud는 AJAX 요청을 구현합니다(코드 포함).

php中世界最好的语言
php中世界最好的语言원래의
2018-03-31 10:38:036420검색

이번에는 AJAX 요청 구현을 위한 apicloud를 가져왔습니다(코드 포함). apicloud에서 AJAX 요청 구현을 위한 주의사항은 무엇인가요?

요청 코드 받기:

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 요청 - 양식 제출:

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 요청 - 다중 부분 데이터, 파일 및 텍스트 필드가 함께 제출됩니다.

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)});
}
});

[End 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=&#39;<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>

이 기사의 사례를 읽은 후 방법을 마스터했다고 믿습니다. 더 흥미로운 정보를 얻으려면 PHP 중국어 웹사이트에서 다른 관련 기사도 주목하세요!

추천 자료:

CORS를 사용하여 WebApi Ajax 도메인 간 요청을 구현하는 방법

ajax 파일 비동기 양식 업로드 구현

위 내용은 apicloud는 AJAX 요청을 구현합니다(코드 포함).의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.