通过 react native 中的 fetch 从请求 api 获取状态和数据
Get status and data from request api by fetch in react native
我尝试在使用此代码时从 React Native 的请求中提取数据和状态请求
function postUser(FirstName, LastName, Phone, Email, Password) {
let data = {
method: 'POST',
credentials: 'same-origin',
mode: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: Email,
password: Password,
first_name: FirstName,
last_name: LastName,
phone: Phone
})
}
return fetch(URLPostUser, data)
.then(response => response.json())
}
在此结果中没有状态。
当更改 return 函数时,我尝试获取状态,但在这种情况下无法访问数据
function postUser(FirstName, LastName, Phone, Email, Password) {
let data = {
method: 'POST',
credentials: 'same-origin',
mode: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: Email,
password: Password,
first_name: FirstName,
last_name: LastName,
phone: Phone
})
}
return fetch(URLPostUser, data)
}
您可以执行以下操作
return fetch(URLPostUser, data)
.then(response => ({ response, json: response.json()}))
现在承诺的对象是
{
response: // the response object
json: // the parsed JSON
}
或者如果您不想要整个回复
return fetch(URLPostUser, data)
.then(response => ({ status: response.status, json: response.json()}))
最终的承诺是对象
{
status: // the response object
json: // the parsed JSON
}
那些 link 对我很有帮助。
因为之前回答return
{ status: 200, json: Promise})
我尝试在使用此代码时从 React Native 的请求中提取数据和状态请求
function postUser(FirstName, LastName, Phone, Email, Password) {
let data = {
method: 'POST',
credentials: 'same-origin',
mode: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: Email,
password: Password,
first_name: FirstName,
last_name: LastName,
phone: Phone
})
}
return fetch(URLPostUser, data)
.then(response => response.json())
}
在此结果中没有状态。
当更改 return 函数时,我尝试获取状态,但在这种情况下无法访问数据
function postUser(FirstName, LastName, Phone, Email, Password) {
let data = {
method: 'POST',
credentials: 'same-origin',
mode: 'same-origin',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
email: Email,
password: Password,
first_name: FirstName,
last_name: LastName,
phone: Phone
})
}
return fetch(URLPostUser, data)
}
您可以执行以下操作
return fetch(URLPostUser, data)
.then(response => ({ response, json: response.json()}))
现在承诺的对象是
{
response: // the response object
json: // the parsed JSON
}
或者如果您不想要整个回复
return fetch(URLPostUser, data)
.then(response => ({ status: response.status, json: response.json()}))
最终的承诺是对象
{
status: // the response object
json: // the parsed JSON
}
那些 link 对我很有帮助。 因为之前回答return
{ status: 200, json: Promise})