ReactJS表单提交this.props.history.push'/ downloads'未链接到新页面

问题描述

一旦用户在下面的函数中使用this.props.history.push填写了表单,我就会尝试将其推送到新页面

public void ThreadProc_Scoring()
{
  List<keyvaluePair<keyvaluePair<DateTime,double>,double>>[] SnapBOUGHT = new List<keyvaluePair<keyvaluePair<DateTime,double>>[27];
  List<keyvaluePair<keyvaluePair<DateTime,double>>[] SnapSOLD = new List<keyvaluePair<keyvaluePair<DateTime,double>>[27];

  int index;
  int normIdx;
  DateTime actual = new DateTime();
  double score = 0;
  double totAscore = 0;
  double totBscore = 0;
  double Asknormalization = 1;
  double Bidnormalization = 1;

  while (true)
  {
    Thread.Sleep(15000);

    //Get a snapshot of BOUGHT/SOLD collections
    //Enter un-interruptible section
    foreach (var symbol in MySymbols)
    {
      index = symbol.Value;
      SnapBOUGHT[index] = BOUGHT[index].ToList();
      SnapSOLD[index] = SOLD[index].ToList();
    }

    //Exit un-interruptible section

    //Do some work on Snapshots
  }

ReactJS表单在/ contacts页面上工作正常,并且将信息提交到我的Django后端,因此我知道它工作正常,但是我无法使重定向正常工作,并且正在向我显示错误消息。

enter image description here

handleSubmit = async event => { event.preventDefault() try { const res = await newEnquiry(this.state.formData) this.props.history.push('/downloads') console.log(res) } catch (err) { console.log(err.response.data) } } 在我的表单标签中,并且工作正常,所以我很确定这对我的表单来说不是问题。

Api.js

<form onSubmit={this.handleSubmit}>

Views.py

const baseUrl = '/api'

export const newEnquiry = formData => {
  return axios.post(`${baseUrl}/enquiries/`,formData)
}

serializers.py

class EnquiryListView(APIView):

    def get(self,_request):
        enquiries = Enquiries.objects.all() 
        serialized_enquiries = EnquirySerializer(enquiries,many=True)
        return Response(serialized_enquiries.data,status=status.HTTP_200_OK)

    def post(self,request):
        created_enquiry = EnquirySerializer(data=request.data)
        if created_enquiry.is_valid():
          created_enquiry.save()
          return Response(created_enquiry.data,status=status.HTTP_201_CREATED)
        return Response(created_enquiry.errors)

解决方法

在您遇到此问题的情况下,当错误触发时,它将无法读取err.response.data属性。如果没有错误,它将重定向您。在您的Django应用中,检查error handler应该返回什么。

快速尝试/捕获。

try {
 // if everything passes,run this block
} catch (err) {
 // if something goes wrong,run this block
}

在这种情况下,请务必检查您的全部错误是什么。可能是404或完全出乎意料的事情。