You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
< template >
< view >
< page -head :title ="title" > < / p a g e - h e a d >
< view class = "uni-padding-wrap uni-common-mt" >
< view class = "uni-hello-text" >
请点击按钮向服务器发起请求
< / view >
< view class = "uni-textarea uni-common-mt" >
< textarea :value ="res" > < / textarea >
< / view >
< view class = "uni-btn-v uni-common-mt" >
< button type = "primary" @click ="sendRequest" :loading ="loading" > 发起请求 ( Callback ) < / button >
< button type = "primary" @click ="sendRequest('promise')" :loading ="loading" > 发起请求 ( Promise ) < / button >
< button type = "primary" @click ="sendRequest('await')" :loading ="loading" > 发起请求 ( Async / Await ) < / button >
< / view >
< / view >
< / view >
< / template >
< script >
const requestUrl = 'http://oa.lanju.cn/lanju/oa/getWorkLog.jsp?requestId=2312706'
const duration = 2000
export default {
data ( ) {
return {
title : 'request' ,
loading : false ,
res : ''
}
} ,
methods : {
sendRequest ( mode ) {
this . loading = true ;
switch ( mode ) {
case 'promise' :
this . _requestPromise ( ) ;
break ;
case 'await' :
this . _requestAwait ( ) ;
break ;
default :
this . _request ( ) ;
break ;
}
} ,
_request ( ) {
uni . request ( {
url : '/prod-api/auth/login' , // 你的API地址
method : 'POST' ,
data : {
username : 'admin' , // 请求的参数
password : 'lanju123'
} ,
header : {
} ,
success : function ( res ) {
console . log ( res . data ) ;
} ,
fail : function ( err ) {
console . log ( err ) ;
} ,
complete : function ( ) {
// 请求结束的处理
}
} ) ;
} ,
_requestPromise ( ) {
uni . request ( {
url : requestUrl ,
dataType : 'text' ,
data : {
noncestr : Date . now ( )
}
} ) . then ( res => {
console . log ( 'request success' , res [ 1 ] ) ;
uni . showToast ( {
title : '请求成功' ,
icon : 'success' ,
mask : true ,
duration : duration
} ) ;
this . res = '请求结果 : ' + JSON . stringify ( res [ 1 ] ) ;
this . loading = false ;
} ) . catch ( err => {
console . log ( 'request fail' , err ) ;
uni . showModal ( {
content : err . errMsg ,
showCancel : false
} ) ;
this . loading = false ;
} ) ;
} ,
async _requestAwait ( ) {
const [ err , res ] = await uni . request ( {
url : requestUrl ,
dataType : 'text' ,
data : {
noncestr : Date . now ( )
}
} ) ;
if ( err ) {
console . log ( 'request fail' , err ) ;
uni . showModal ( {
content : err . errMsg ,
showCancel : false
} ) ;
} else {
console . log ( 'request success' , res )
uni . showToast ( {
title : '请求成功' ,
icon : 'success' ,
mask : true ,
duration : duration
} ) ;
this . res = '请求结果 : ' + JSON . stringify ( res ) ;
}
this . loading = false ;
}
}
}
< / script >