在第二个提取请求中使用提取响应
use a fetch response in a second fetch request
我有一个包含两个提取请求的函数。第一个获取请求 returns 包含 ACCTNO 的对象数组。我正在尝试使用返回的 ACCTNO 是第二个请求。但是我没有取得任何成功。我还尝试引用 this.state.buildUrl,因为我需要 buildUrl 在视图中呈现,但我收到错误提示 this.setState 不是函数或未定义。如何在我的第二个请求中使用第一个提取响应?
componentDidMount() {
var url = 'http://10.0.2.2:8080/combined';
var result = fetch(url, {
method: 'get',
}).then(function(response) {
return response.json();
}).then(function(data) {
var ACCTNO = data[0].ACCOUNT;
var FormData = require('form-data');
var form = new FormData();
form.append('businessId', '123456');
form.append('login', 'SAFConnectUser');
form.append('password', '123445678');
form.append('billingAccountNumber', ACCTNO);
form.append('signOffURL', 'fd');
form.append('timeOutURL', 'fd');
fetch('https://collectpay-uat.princetonecom.com/otp/namevaluepair/submitSingleSignOn.do', {
method: 'POST',
body: form
}).then((response) => response.text()).then((res) => {
if (res.error) {
console.log(res.error)
}
console.log(res)
var fields = res.split('|');
var getUrl = fields[3];
var buildUrl = getUrl.slice(14)
// Getting error that setState is not a function?
this.setState({buildUrl})
console.log(buildUrl)
console.log(ACCTNO)
})
})
}
render() {
return (
<WebView
source={{uri: this.state.buildUrl}}
style={{marginTop: 20}}
/>
);
}
}
export default acidirect;
您可以将第一次获取状态的响应设置为
fetch('/url',{credentials: 'same-origin'}).then(function(response) {
return response.json();
}).then(this.setSomeState);
现在设置你的状态
setSomeState: function(response) {
this.setState({
array: response
});
},
现在您可以在任何地方使用
this.state.array
把你所有的函数都改成粗箭头函数,这样它们就可以参考this
:
class acidirect {
componentDidMount() {
fetch('http://10.0.2.2:8080/combined', {
method: 'get',
})
.then(response => response.json())
.then(data => {
var ACCTNO = data[0].ACCOUNT;
var FormData = require('form-data');
var form = new FormData();
form.append('businessId', '123456');
form.append('login', 'SAFConnectUser');
form.append('password', '123445678');
form.append('billingAccountNumber', ACCTNO);
form.append('signOffURL', 'fd');
form.append('timeOutURL', 'fd');
fetch('https://collectpay-uat.princetonecom.com/otp/namevaluepair/submitSingleSignOn.do', {
method: 'POST',
body: form
})
.then(response => response.text())
.then(res => {
var fields = res.split('|');
var getUrl = fields[3];
var buildUrl = getUrl.slice(14);
this.setState({buildUrl});
});
});
}
render() {
return (
<WebView
source={{uri: this.state.buildUrl}}
style={{marginTop: 20}}
/>
);
}
}
export default acidirect;
我有一个包含两个提取请求的函数。第一个获取请求 returns 包含 ACCTNO 的对象数组。我正在尝试使用返回的 ACCTNO 是第二个请求。但是我没有取得任何成功。我还尝试引用 this.state.buildUrl,因为我需要 buildUrl 在视图中呈现,但我收到错误提示 this.setState 不是函数或未定义。如何在我的第二个请求中使用第一个提取响应?
componentDidMount() {
var url = 'http://10.0.2.2:8080/combined';
var result = fetch(url, {
method: 'get',
}).then(function(response) {
return response.json();
}).then(function(data) {
var ACCTNO = data[0].ACCOUNT;
var FormData = require('form-data');
var form = new FormData();
form.append('businessId', '123456');
form.append('login', 'SAFConnectUser');
form.append('password', '123445678');
form.append('billingAccountNumber', ACCTNO);
form.append('signOffURL', 'fd');
form.append('timeOutURL', 'fd');
fetch('https://collectpay-uat.princetonecom.com/otp/namevaluepair/submitSingleSignOn.do', {
method: 'POST',
body: form
}).then((response) => response.text()).then((res) => {
if (res.error) {
console.log(res.error)
}
console.log(res)
var fields = res.split('|');
var getUrl = fields[3];
var buildUrl = getUrl.slice(14)
// Getting error that setState is not a function?
this.setState({buildUrl})
console.log(buildUrl)
console.log(ACCTNO)
})
})
}
render() {
return (
<WebView
source={{uri: this.state.buildUrl}}
style={{marginTop: 20}}
/>
);
}
}
export default acidirect;
您可以将第一次获取状态的响应设置为
fetch('/url',{credentials: 'same-origin'}).then(function(response) {
return response.json();
}).then(this.setSomeState);
现在设置你的状态
setSomeState: function(response) {
this.setState({
array: response
});
},
现在您可以在任何地方使用
this.state.array
把你所有的函数都改成粗箭头函数,这样它们就可以参考this
:
class acidirect {
componentDidMount() {
fetch('http://10.0.2.2:8080/combined', {
method: 'get',
})
.then(response => response.json())
.then(data => {
var ACCTNO = data[0].ACCOUNT;
var FormData = require('form-data');
var form = new FormData();
form.append('businessId', '123456');
form.append('login', 'SAFConnectUser');
form.append('password', '123445678');
form.append('billingAccountNumber', ACCTNO);
form.append('signOffURL', 'fd');
form.append('timeOutURL', 'fd');
fetch('https://collectpay-uat.princetonecom.com/otp/namevaluepair/submitSingleSignOn.do', {
method: 'POST',
body: form
})
.then(response => response.text())
.then(res => {
var fields = res.split('|');
var getUrl = fields[3];
var buildUrl = getUrl.slice(14);
this.setState({buildUrl});
});
});
}
render() {
return (
<WebView
source={{uri: this.state.buildUrl}}
style={{marginTop: 20}}
/>
);
}
}
export default acidirect;