小编kam*_*fox的帖子

如何从函数返回 XMLHttpRequest 响应?

值未替换并且函数返回 0。如何修复它?(react-native 0.30,IOS 10.0模拟器)

export function getCategoryList() {
  var xhr = new XMLHttpRequest();
  jsonResponse = null;

  xhr.onreadystatechange = (e) => {
    if (xhr.readyState !== 4) {
      return;
    }

    if (xhr.status === 200) {
      console.log('SUCCESS', xhr.responseText);
      jsonResponse = JSON.parse(xhr.responseText);
    } else {
      console.warn('request_error');
    }
  };

  xhr.open('GET', 'https://httpbin.org/user-agent');
  xhr.send();

  return jsonResponse;
}
Run Code Online (Sandbox Code Playgroud)

javascript networking xmlhttprequest react-native

7
推荐指数
1
解决办法
1万
查看次数