作为 object 的一部分发送时无法读取 response.json()
cannot read response.json() when sent as part of an object
我正在从函数进行 API 调用,return response.json()
,在调用函数上我可以获得数据。但是除了 response.json() 我还需要将 header
数据传递给调用函数所以我创建了一个 object 并添加了 response.json() 和 header 数据
在调用函数中我可以读取 header
数据但不能读取 response.json()
。你能建议如何正确读取响应数据吗?
API调用函数
export function loginlibAPI ( id, password ){
var loginURL = 'https://example.com/sessions';
return fetch(loginURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"user": {
'email': id,
'password': password
}
}),
})
.then((response) =>{
consoleLog('loginUser_lib - header ' + response.headers.get('Authorization-X'));
if (response.headers.get('content-type').match(/application\/json/)) {
consoleLog('inside content type');
//return response.json(); // works
return { response: response.json(), authorizationToken: response.headers.get('Authorization-X') }; //can not read the response in the calling function
}
return response;
})
.catch((error) => {
consoleLog('Error from loginlibAPI() api call - ' + error.message);
});
}
调用函数
loginUser_lib = async ( ) => {
const returned = await loginlibAPI( this.state.nationalId, this.state.password ).then((res) => {
//consoleLog('loginUser_lib - ' + JSON.stringify(res)); // can read data when only response.json() is sent
consoleLog('loginUser_lib - ' + res.authorizationToken); //works fine - received 'abcdfdlkjsdlkjsdlkj'
consoleLog('loginUser_lib - ' + res.response); //returns - [object Object]
consoleLog('loginUser_lib - ' + JSON.stringify(res.response)); //returns - {"_40":0,"_65":0,"_55":null,"_72":null}
})
}
请记住 response.json()
returns 一个 承诺 ,因此您需要在从中获取数据之前解决它。类似于:
return response
.json()
.then(data => ({
response: data,
authorizationToken: response.headers.get('Authorization-X')
});
我正在从函数进行 API 调用,return response.json()
,在调用函数上我可以获得数据。但是除了 response.json() 我还需要将 header
数据传递给调用函数所以我创建了一个 object 并添加了 response.json() 和 header 数据
在调用函数中我可以读取 header
数据但不能读取 response.json()
。你能建议如何正确读取响应数据吗?
API调用函数
export function loginlibAPI ( id, password ){
var loginURL = 'https://example.com/sessions';
return fetch(loginURL, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
"user": {
'email': id,
'password': password
}
}),
})
.then((response) =>{
consoleLog('loginUser_lib - header ' + response.headers.get('Authorization-X'));
if (response.headers.get('content-type').match(/application\/json/)) {
consoleLog('inside content type');
//return response.json(); // works
return { response: response.json(), authorizationToken: response.headers.get('Authorization-X') }; //can not read the response in the calling function
}
return response;
})
.catch((error) => {
consoleLog('Error from loginlibAPI() api call - ' + error.message);
});
}
调用函数
loginUser_lib = async ( ) => {
const returned = await loginlibAPI( this.state.nationalId, this.state.password ).then((res) => {
//consoleLog('loginUser_lib - ' + JSON.stringify(res)); // can read data when only response.json() is sent
consoleLog('loginUser_lib - ' + res.authorizationToken); //works fine - received 'abcdfdlkjsdlkjsdlkj'
consoleLog('loginUser_lib - ' + res.response); //returns - [object Object]
consoleLog('loginUser_lib - ' + JSON.stringify(res.response)); //returns - {"_40":0,"_65":0,"_55":null,"_72":null}
})
}
请记住 response.json()
returns 一个 承诺 ,因此您需要在从中获取数据之前解决它。类似于:
return response
.json()
.then(data => ({
response: data,
authorizationToken: response.headers.get('Authorization-X')
});