无法在request.post方法中设置状态[duplicate](can't setState in request.post method [duplicate])

编程入门 行业动态 更新时间:2024-10-19 09:31:48
无法在request.post方法中设置状态[duplicate](can't setState in request.post method [duplicate])

这个问题在这里已经有了答案:

如何在回调中访问正确的`this`? 6个答案

你好,我试图设置从我的后端到状态的响应。 但我总是得到这个错误。

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); }) }

This question already has an answer here:

How to access the correct `this` inside a callback? 10 answers

Hello i'm trying to set the response i'am getting from my backend to State. But i allways get this error.

TypeError: this.setState is not a function

can someone pls tell me how i can set my response to the state ?

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); })

Use a arrow function for the callback.

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); })

更多推荐

本文发布于:2023-08-04 11:14:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1415272.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:状态   方法   post   request   setState

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!