How to submit Pardot form handlers with XMLHttpRequest?

439 views Asked by At

I am confused, why does pardot not allow such AJAX requests? aren't they more user friendly features?

I am trying to add a form handler and tried to use it of course & it works when I keep the default submission of pardot forms handler. ( a simple click page redirected to success URL , else to error url)

When I try to do so with XMLHttpRequest I get this error:

index.html:1 Access to XMLHttpRequest at 'http://go.pardot.com/my-long-url-here' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

This is my code:

    var xhr = new XMLHttpRequest();

    xhr.open("POST", 'http://go.pardot.com/l/903091/2021-02-05/4cqj', true);
    xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
    //Send the proper header information along with the request
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    xhr.onreadystatechange = function () { // Call a function when the state changes.
        if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {
            // Request finished. Do processing here.
        } else {
            alert("error of course")
        }
    }
    xhr.send("[email protected]");
    // xhr.send(new Int8Array());
    // xhr.send(document);

The form has just a simple email.

Is there a way to make this work with AJAX?

0

There are 0 answers