Ajax React native:无法处理的提取响应

Ajax React native:无法处理的提取响应,ajax,react-native,state,fetch,setstate,Ajax,React Native,State,Fetch,Setstate,仍在学习RN。。。在智能手机浏览器中打开网页之前,我试图在react native中使用fetch(),从我的服务器中获取特定数据。 以下是我写的: openLink = () => { //Communicate to the server to get an unique key_id this.state = {urlKey: 'text'}; //Initial state var params = { // Some params send b

仍在学习RN。。。在智能手机浏览器中打开网页之前,我试图在react native中使用
fetch()
,从我的服务器中获取特定数据。 以下是我写的:

openLink = () => {       //Communicate to the server to get an unique key_id
  this.state = {urlKey: 'text'}; //Initial state


  var params = {
      // Some params send by POST to authenticate the request...
  };

  var formData = new FormData();

  for (var k in params) {
      formData.append(k, params[k]);
  }
      fetch(Constants.URL.root+"mobile/authorize_view", {
                method: 'POST',
                headers: {
                 'Accept': 'application/json',
                 'Content-Type': 'multipart/form-data',
               },
                body: formData
              })
          .then((response) => response.json())
          .then((responseJson) => {
            this.setState({urlKey:responseJson.document_key}); //Getting the response, and changing the initial state (was 'text' previously)
          })
          .done();

  var urlString = Constants.URL.upload + '/' + this.state.urlKey; // !!Problem : opening in browser with this.state.urlKey = text, and not document_key!!
  Linking.canOpenURL(urlString).then(supported => {
    if (supported) {
      Linking.openURL(urlString);
    } else {
      console.log('Don\'t know how to open URI: ' + this.props.url);
    }
  });
}
实际上,正如您所看到的,我要求为我的服务器提供一个特定的键(urlKey,它在JSON对象中返回:responseJson.document\u key)

服务器部分的一切都运行良好,因为我将生成的文档密钥放入了数据库中,我可以看到它被正确放置

问题出在React原生部分:浏览器打开一个网页,其中
this.state.urlKey
**text**
,这是初始状态,函数
fetch
应该变成服务器发送的文档密钥。。。
我缺少什么?

fetch语句是异步的。这意味着当您调用fetch时,则下一行执行不需要
。然后
,而是
var urlString=Constants.URL.upload+'/'+this.state.urlKey

在此阶段注意,如果
。那么
未完成获取数据,您的
此.state.document\u键
将不会填充。因此,您会看到错误

相反,在最后的
中移动该代码,然后
,例如:

openLink = () => {       //Communicate to the server to get an unique key_id
  this.state = {urlKey: 'text'}; //Initial state


  var params = {
      // Some params send by POST to authenticate the request...
  };

  var formData = new FormData();

  for (var k in params) {
      formData.append(k, params[k]);
  }
      fetch(Constants.URL.root+"mobile/authorize_view", {
                method: 'POST',
                headers: {
                 'Accept': 'application/json',
                 'Content-Type': 'multipart/form-data',
               },
                body: formData
              })
          .then((response) => response.json())
          .then((responseJson) => {
            this.setState({urlKey:responseJson.document_key}); //Getting the response, and changing the initial state (was 'text' previously)
              //moved inside then
              var urlString = Constants.URL.upload + '/' + this.state.urlKey; // !!Problem : opening in browser with this.state.urlKey = text, and not document_key!!
              Linking.canOpenURL(urlString).then(supported => {
                if (supported) {
                  Linking.openURL(urlString);
                } else {
                  console.log('Don\'t know how to open URI: ' + this.props.url);
                }
              });
          })
          .done();
}