3

How to post data and target the returned response to iframe using axios as it is done with form submit.

What i have done is I have data that needs to be pushed to backend asp.net core mvc core application which redirect to another action method which return the view.

The view returned from above is targetted to iframe below is the code

         <form
           ref={r => {
             formRef = r;
           }}
           action={getSourceUrl()} // https://something.com/actionmethod
           target="targetframe"
           method="post"
           >
           <input type="hidden" value="random1" name="token" id="token" />
           <input type="hidden" value="random2" name="domain" id="domain" />
        </form>

        <iframe
          src=""
          name="targetframe"
          onLoad={() => iframeLoaded()}
        />

formRef.submit() //called after DOM mounted

The above code works perfectly fine but what if the api fails.. there is no way to catch the failure of the api from submit call if you can help me, like if there is a way for this than that would be the answer but since i have seen some post which states there is no way to catch submit failure so i am trying the axios route so with the help of ajax, axios, fetch or any library can i achieve the above thing since using these libraries i get promise whether the api failed or not and i can do some error handling?

Ans
  • 344
  • 1
  • 4
  • 11
  • if you want to use axios for making HTTP calls, you should check - [Axios Post Form with Reactjs](https://stackoverflow.com/q/50617966/2873538) and [axios docs](https://github.com/axios/axios). And [here](https://stackoverflow.com/a/61987408/2873538), you can see how to make and handle/submit a form in react. – Ajeet Shah May 28 '20 at 16:07
  • 1
    but the question is not about axios post request , its about doing axios post request and target the response to iframe or a way to handle submit form failure – Ans May 28 '20 at 17:41

0 Answers0