-5

Say I have a web page and I want it to automatically "go to" some form. This sort of thing works fine:

script(type = 'text/javascript').
    var ff = document.createElement("form")
    document.body.appendChild(ff)

    var ii = document.createElement("input")
    ii.setAttribute("type","hidden")
    ii.setAttribute("height", 69)
    ff.appendChild(ii)

    ff.method = "POST"
    ff.action = window.location + "/someform"
    ff.submit()

However, it's much easier to use fetch to send a form:

script(type = 'text/javascript').
    fetch("/someform", {
    method: "POST",
    headers: {'Content-Type': 'application/json'},
    body: JSON.stringify({height: 69})
    })

However that does not actually "go to" the new page, /someform.

Is there actually a way to make fetch "go to" the form in question, rather than just fetch the results behind the scene?

9
  • 5
    If the goal is to submit the form normally then why use AJAX at all? It sounds like simply removing that functionality and using a normal HTML form is what you want. (As an aside... What is script(type = 'text/javascript').? Additionally, you should definitely get in the habit of terminating statements with semicolons. Omitting those commonly leads to strange bugs.) Commented May 12 at 16:52
  • @David Cheers, I assumed it was obvious I meant .. "I want it to automatically go to some form ...", thanks I edited that in case it causes confusion. The question is about fetch .. is there actually a way to make fetch "go to" the form in question, rather than just fetch the results behind the scene? TY Commented May 12 at 16:55
  • 1
    The simple answer is no. When you use AJAX, the browser doesn't do anything automatically. The JS code decides what to do. It could do a redirect after getting the response, but that won't be the same as what gets returned by the form action. Commented May 12 at 16:56
  • @Fattie: Well, that depends on the specifics of what you want to do. fetch doesn't perform a navigation. But if you want to perform a navigation then you can do that in JavaScript. Have you tried that? Commented May 12 at 16:56
  • Hey @Barmar ! Good one, a clear "No" answer is is often so helpful. You should put it in. Commented May 12 at 16:57

2 Answers 2

4

No, there's no way to make fetch() automatically navigate. In general, we use AJAX because we don't want to reload the page with the form response; if that's what you want, use form.submit().

You can navigate away after getting the AJAX response, but that won't generally be the same as reloading with the HTML returned by the action URL.

Sign up to request clarification or add additional context in comments.

Comments

0
+50

The answer is Yes!

You can get the contents from fetch, change the visible location with function history.pushState() and totally replace document.documentElement.innerHTML with fetched data. The only thing that will be lost - some http headers which could affect the behavior of the page.

The answer is No!

In most cases once you received the response from server on your POST-request its state will never be the same. If you just walk to new location /someform server will not receive POST and the reply will differ from the reply you already consumed by fetch. Of course there are some servers that always reply the same ( something like 'OK'). But don't get your hopes up.

In both cases be ready to get some bounties from CORS :•)

So, which answer do you prefer?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.