2016-09-17 101 views
0

我有这个简单的网络请求:阵营母语:网络请求失败,但实际上并没有

registerDevice(data) { 
    return new Promise((next, error) => { 
     fetch(SERVER_URL + '/devices', { 
     method: 'POST', 
     headers: { 
      'Content-Type': 'application/json', 
     }, 
     body: JSON.stringify(data) 
     }) 
     .then((response) => { 
      return response.json() 
     }) 
     .then((responseData) => { 
      console.log(responseData); 
      next(responseData); 
     }) 
     .catch((err) => error(err)); 
    }) 
    } 

每次被执行的时候,我得到一个

Network request failed 
at XMLHttpRequest.xhr.onerror... 

虽然有没有错误,数据成功发布,我得到正确的响应标题。

RN是0.33,我正在为Android编译。 Android版本4.4.4,5.0.1,6.0.1和7.0上的错误

+0

“XMLHttpRequest”在哪里使用? – guest271314

+0

https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API –

+0

您参考了哪部分链接的文档? – guest271314

回答

0

您可以使用ES6语法。您的代码看起来就像这样:

fetch(url,{method:'POST',body:data}) 
.then(r => r.json()) 
.then(data => console.log(data)) 
.catch(e => console.log("Booo")); 

编号:https://jakearchibald.com/2015/thats-so-fetch/

== ==编辑

对不起,也许你可以尝试使用awaitasync。代码:

let response = await fetch(this.api_url,{ 
    method:'POST', 
    body: data 
}) 

let responseJson = await response.json(); 
return responseJson; 

你能解释一下你收到的错误吗?

+0

请再看看我写的内容,并告诉我它是不是ES6语法。 –

+0

我尝试在android上,它运作良好,你可以扩大你的错误输出? – leo7r

相关问题