【发布时间】:2018-06-13 05:54:21
【问题描述】:
我正在尝试调用fetch() 函数。
它在 iOS 上运行良好,但在 Android 上无法运行。
fetch('https://admin.leanpoint.com/Api/User/Login?user=srinu@mainpoint.dk', {
method: 'POST',
headers: {
'Authorization': 'Basic c3JpbnVAbWFpbnBvaW50LmRrOnNhaWJhYmE='
},
})
.then((res) => {
res.json().then((json) => {
alert(JSON.stringify(json))
})
})
.catch((err) => {
alert(JSON.stringify(err))
})
在 iOS 上,它输入 .then(),但在 Android 上,它输入 .catch(),而 err 是 {}。
它不适用于模拟器和真实设备。
感谢任何帮助。 ^_^
使用 Axios 的错误日志
当我使用 fetch() 时,它使用 {} 输入 .catch()。
我尝试了Axios,如下所示。
axios.post('https://admin.leanpoint.com/Api/User/Login?user=srinu@mainpoint.dk', null, {
headers: {
'Authorization': 'Basic c3JpbnVAbWFpbnBvaW50LmRrOnNhaWJhYmE='
},
timeout: 2000
})
.then((res) => {
console.log(res.data)
})
.catch((err) => {
console.log(err)
})
它返回如下错误。
{ [Error: Network Error]
config:
{ adapter: [Function: xhrAdapter],
transformRequest: { '0': [Function: transformRequest] },
transformResponse: { '0': [Function: transformResponse] },
timeout: 2000,
xsrfCookieName: 'XSRF-TOKEN',
xsrfHeaderName: 'X-XSRF-TOKEN',
maxContentLength: -1,
validateStatus: [Function: validateStatus],
headers:
{ Accept: 'application/json, text/plain, */*',
'Content-Type': 'application/x-www-form-urlencoded',
Authorization: 'Basic c3JpbnVAbWFpbnBvaW50LmRrOnNhaWJhYmE=' },
method: 'post',
url: 'https://admin.leanpoint.com/Api/User/Login?user=srinu@mainpoint.dk',
data: null },
request:
{ UNSENT: 0,
OPENED: 1,
HEADERS_RECEIVED: 2,
LOADING: 3,
DONE: 4,
readyState: 4,
status: 0,
timeout: 2000,
withCredentials: true,
upload: {},
_aborted: false,
_hasError: true,
_method: 'POST',
_response: 'java.security.cert.CertPathValidatorException: Trust anchor for certification path not found.',
_url: 'https://admin.leanpoint.com/Api/User/Login?user=srinu@mainpoint.dk',
_timedOut: false,
_trackingName: 'unknown',
_incrementalEvents: false,
responseHeaders: undefined,
_requestId: null,
_cachedResponse: undefined,
_headers:
{ accept: 'application/json, text/plain, */*',
'content-type': 'application/x-www-form-urlencoded',
authorization: 'Basic c3JpbnVAbWFpbnBvaW50LmRrOnNhaWJhYmE=' },
_responseType: '',
_sent: true,
_lowerCaseResponseHeaders: {},
_subscriptions: [] },
response: undefined }
【问题讨论】:
-
在 android manifest 文件中添加了 INTERNET 权限?
-
是的,我在清单中添加了
<uses-permission android:name="android.permission.INTERNET" /> -
我不确定如何在 React Native 中使用。你能在这里为上面的 fetch() 函数写一些代码吗?
-
嗯。更新清单后是否重新安装了应用?
标签: javascript android react-native fetch