Jump to content

Inject XMLHttpRequest before form post

Recommended Posts

Hello,

I'm trying to inject a piece of Javascript code in to a page that has a form. What I want is to run the Javascript code with an XMLHttpRequest and wait for a response (onloadend) and then continue with the form post to the Squarespace servers.

I can pause the execution of the form post, but I don't know how to resume it!

I've tried things like:

document.getElementsByClassName(the button class)[0].click()

and

document.querySelector('[data-form-id="<form id>"]').submit()

They give 403 Forbidden

 

 

How would I do it?

 

Thanks!

 

Link to comment
  • Replies 3
  • Created
  • Last Reply

Top Posters In This Topic

As per my understanding, what you want to try is inject an Ajax request before submitting the form.

But my question is why you want to do that, if you are doing that for validation then you won't use Ajax request for that, server sends the error response and you just need to display the errors.

Now, if you want to send the Ajax request then you have to submit the form manually using JavaScript.

When the user clicks the submit button, you preventDefault() in the function and then send your Ajax request to squarespace server and it will return the response and then you write the code that submit the post request to the server with form data.

Link to comment
On 8/19/2020 at 5:39 PM, ankitlathiya said:

As per my understanding, what you want to try is inject an Ajax request before submitting the form.

But my question is why you want to do that, if you are doing that for validation then you won't use Ajax request for that, server sends the error response and you just need to display the errors.

Now, if you want to send the Ajax request then you have to submit the form manually using JavaScript.

When the user clicks the submit button, you preventDefault() in the function and then send your Ajax request to squarespace server and it will return the response and then you write the code that submit the post request to the server with form data.

How can I add an ajax to my form? I tried adding a listener to the submit button but it didn't trigger. Can you share a code sample? I've added jquery to my site too.

Link to comment
  • 2 months later...
On 8/19/2020 at 7:39 PM, ankitlathiya said:

As per my understanding, what you want to try is inject an Ajax request before submitting the form.

But my question is why you want to do that, if you are doing that for validation then you won't use Ajax request for that, server sends the error response and you just need to display the errors.

Now, if you want to send the Ajax request then you have to submit the form manually using JavaScript.

When the user clicks the submit button, you preventDefault() in the function and then send your Ajax request to squarespace server and it will return the response and then you write the code that submit the post request to the server with form data.

Hi, thank you for your response!

Its not related to validating the form, its for sending the data to another server also.

Yes I can use preventDefault() like I said in my original post: "I can pause the execution of the form post".

But I cannot resume it.

How do I "then you write the code that submit the post request to the server with form data." ? Thats my big nut to crack!

 

Thank you

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

×
×
  • Create New...

Squarespace Webinars

Free online sessions where you’ll learn the basics and refine your Squarespace skills.

Hire a Designer

Stand out online with the help of an experienced designer or developer.