在request.post方法中不能setState

时间:2018-05-18 14:26:37

标签: javascript reactjs ecmascript-6 request response

您好我正试图设置我从后端到州的响应。但我总是得到这个错误。

  

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

 }

1 个答案:

答案 0 :(得分:1)

使用箭头功能进行回调。

private JsonResult GetProcesedXml()
{
     JObject json = null;
     Stream request = Request.InputStream;
     using (StreamReader sr = new StreamReader(stream))
     {
           stream.Seek(0, System.IO.SeekOrigin.Begin);
           json = JObject.Parse(sr.ReadToEnd());
     }
     string xmlSigninigResult = json["resultXml"].ToString();
     // rest of the method
}