bla*_*eve 9 javascript android fetch react-native
我想调用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))
})
Run Code Online (Sandbox Code Playgroud)
在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)
})
Run Code Online (Sandbox Code Playgroud)
并返回如下错误.
{ [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 }
Run Code Online (Sandbox Code Playgroud)
这对我有用
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<application android:usesCleartextTraffic="true" tools:targetApi="28" />
Run Code Online (Sandbox Code Playgroud)
《我是蝙蝠侠》提出了使用Axios 的好建议。Axios 要简单得多,并且在 ios/android 上都能一致工作,而不是出现这些奇怪的错误。下面是直接从他们的 github 发出 POST 请求的示例:
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
7631 次 |
最近记录: |