javascript - 无法在 request.post 方法中设置状态
问题描述
您好,我正在尝试将我从后端获得的响应设置为状态。但我总是得到这个错误。
TypeError:this.setState 不是函数
有人可以告诉我如何设置对状态的响应吗?
recalcRequest(data){
let meth = data
let brandCode = meth.brandCode
let paymentAmount = this.props.location.query.anfrage.umsatz+"00"
let merchantReference = this.props.location.query.cardid
let jsonData = {
brandCode : brandCode,
paymentAmount : paymentAmount,
merchantReference:merchantReference
};
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'http://localhost:8888/Backend/recalcSig.php',
body: "data=" + JSON.stringify(jsonData)
}, function(err, response, body){
if (err) throw err;
body = JSON.parse(body)
this.setState({
merchantSig: body.merchantSig,
sessionValidity: body.request.sessionValidity,
requestPaymentMethods: false,
showCardDetails:true
})
console.log('response: ', response);
})
}
解决方案
使用箭头函数进行回调。
request.post({
headers: {'content-type' : 'application/x-www-form-urlencoded'},
url: 'http://localhost:8888/Backend/recalcSig.php',
body: "data=" + JSON.stringify(jsonData)
},(err, response, body) => {
if (err) throw err;
body = JSON.parse(body)
this.setState({
merchantSig: body.merchantSig,
sessionValidity: body.request.sessionValidity,
requestPaymentMethods: false,
showCardDetails:true
})
console.log('response: ', response);
})
推荐阅读
- javascript - 如何使 node.js 应用程序以编程方式永久运行
- amazon-web-services - AWS ec2 ubuntu+nginx+uwgi+flask 深度学习 api: 504 Gateway Time-out
- arduino - Arduino旋转编码器定时器
- powershell - DevOps Powershell 步骤成功调用 bat 文件但返回 1
- c# - 生成 1 到 10 之间的唯一随机数?
- python - 获取每个合约的唯一 custId 数量
- singlestore - MemSQL INFORMATION_SCHEMA.COLUMNAR_SEGMENTS 报告的空间使用情况是否包含冗余?
- c# - 使用异步 lambda 表达式正确等待 Task.Run
- javascript - 关于集群和子进程如何工作的困惑
- php - Laravel AWS SES - 403 Forbidden - SignatureDoesNotMatch 尝试发送电子邮件时