react native 之异步请求

第一章 异步请求  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")
      });


}

注意参数的写法与回调函数的写法.

 

第二章 异步请求的优化

 

posted @ 2016-08-12 17:50  雪昀  阅读(3962)  评论(0编辑  收藏  举报