第一章 異步請求 fetch的運用
在react native 中異步請求一般用fetch這個方法, fetch的格式如下:
const params ={
"charset":"utf-8","sessionToken":"dedbde5567e444a2b71b36ba7af9d7c5",
"sessionRandom":"","requestNo":"99999991470815595421","channelCode":"001",
"clientId":"9999999","transCode":"99002",
"requestBodyJson":"{\"mobile\":\"13311223322\",\"password\":\"000000\",\"deviceNo\":\"1470815595420##\",\"requestType\":\"0\"}",
"mobile":"13311223322","empNo":"","devicesn":"1470815595421","url":"/access/doSubmit.do"
}
function login(username, password, success, failure) {
fetch('http://bxxsit.cpic.com.cn/sxtbweb/service/access/doSubmit.do',{
method :'POST',
headers:{
'Accept': 'application/json',
'Content-Type': 'application/json',
},
body:JSON.stringify(params)
})
.then((response) =>response.json())
.then((responseJson) => {
alert(JSON.stringify(responseJson))
AsyncStorage.setItem('message',JSON.stringify(responseJson),()=>{
// AsyncStorage.mergeItem('person',JSON.stringify(getState()),() =>{
//});
})
})
.catch((error) => {
alert ("Failed")
});
}
注意參數的寫法與回調函數的寫法.
第二章 異步請求的優化