Javascript 通过获取react native从请求api获取状态和数据
当使用此代码时,我尝试从react native中的请求中提取数据和状态请求Javascript 通过获取react native从请求api获取状态和数据,javascript,react-native,fetch,Javascript,React Native,Fetch,当使用此代码时,我尝试从react native中的请求中提取数据和状态请求 function postUser(FirstName, LastName, Phone, Email, Password) { let data = { method: 'POST', credentials: 'same-origin', mode: 'same-origin', headers: { 'Accept':
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())
}
在这个结果中,你没有地位
当更改函数的返回时,我尝试获取状态,但在这种情况下,我无法访问数据
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
}
这些链接对我很有帮助。
因为以前回答过返回
{status:200,json:Promise})