【发布时间】:2021-01-09 23:49:06
【问题描述】:
export const login = () => {
return async (dispatch) => {
try {
const response = await fetch(
`https://simplyrem.com/srwl/haib/index.php`,
{
method: 'POST',
headers: {
accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
auth: 'XCXCXCXCX',
usernamex: 'XXXXX',
passwordx: 'XXXXXXXX',
}),
}
)
if (!response.ok) {
const errorResData = await response.json()
console.log(errorResData)
throw new Error('Something went wrong!')
}
const responseData = await response.json()
const userInfo = responseData
console.log(userInfo)
dispatch({
type: LOGIN,
userInfo: userInfo,
})
} catch (err) {
throw err
}
}
}
大家好,
我在输入我的凭据时使用 insomnia 测试了这个 API,我收到了 JSON 响应,所以我知道这个 API 可以正常工作并且我的凭据是正确的。我是 React Native 的新手,我不知道我是否通过使用 body: JSON.stringify 正确添加了参数......显然,我已经用“X”替换了信息。在 swift 中,我也能够得到响应,但是当我尝试在 react-native 中重新创建 API 调用时出现错误
> [Unhandled promise rejection: SyntaxError: JSON Parse error: Unexpected EOF]
> * [native code]:null in parse
> - node_modules/react-native/node_modules/promise/setimmediate/core.js:37:13 in tryCallOne
> - node_modules/react-native/node_modules/promise/setimmediate/core.js:123:24 in setImmediate$argument_0
> - node_modules/react-native/Libraries/Core/Timers/JSTimers.js:130:14 in _callTimer
> - node_modules/react-native/Libraries/Core/Timers/JSTimers.js:181:14 in _callImmediatesPass
> - node_modules/react-native/Libraries/Core/Timers/JSTimers.js:441:30 in callImmediates
> - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:387:6 in __callImmediates
> - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:135:6 in __guard$argument_0
> - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:364:10 in __guard
> - node_modules/react-native/Libraries/BatchedBridge/MessageQueue.js:134:4 in flushedQueue
> * [native code]:null in flushedQueue
> * [native code]:null in invokeCallbackAndReturnFlushedQueue >
【问题讨论】:
标签: java node.js reactjs react-native