POST Request; Form; Firebase; Cloud Function

Hi all, sorry if this is a double post I wasn’t sure what topic was best to post under.

I have created a cloud function to send an SMS when a user submits a form. I have set the action to the cloud function URL but not sure what else I have to do. I have included a photo of this working in another tool but don’t know exactly how to translate to webflow.

I’ve added this custom code before </body and still having issues

UPDATE: Below is the working custom code to send a POST request to a cloud function.

<script type="text/javascript">
        var button = document.getElementById('smsBtn');
        function sendSMS() {
          var phoneNumber = document.getElementById('phoneNum').value;
            $.ajax({
                url: 'https://us-central1-newapp-prod.cloudfunctions.net/textToDownload',
                type: 'post',
                data: JSON.stringify({
                    "data": {
                        "phone": phoneNumber
                    }
                }),
                contentType: 'application/json',
                dataType: 'json',
                success: function (data) {
                    console.info(data);
                }
            });
          console.log(phoneNumber)
        }
        button.addEventListener("click", event => {
            console.log("clicked");
            sendSMS();
        });
</script>

I appreciate any and all feedback and help

Here is my site Read-Only: https://preview.webflow.com/preview/paway-landing?utm_medium=preview_link&utm_source=designer&utm_content=paway-landing&preview=4ef45c69059cf9f84f04c741764c5990&mode=preview

Hey Matthew

Take a look at this StackOverflow answer: https://stackoverflow.com/a/22195153

You should also target the form element with a submit eventhandler and maybe prevent default.

1 Like

Hey @Davidlin_ch12 thanks for your response it was definitely helpful. I’ve updated my custom code in the original question and it seems to be working but am getting a CORS issue. Waiting for my colleague to update the response header to test. If I have any other issues I may ping you. Thanks again!

1 Like

Here is the solution I came to, I hope this helps someone else!

<script type="text/javascript">
        var button = document.getElementById('smsBtn');
        function sendSMS() {
          var phoneNumber = document.getElementById('phoneNum').value;
            $.ajax({
                url: 'https://us-central1-newapp-prod.cloudfunctions.net/textToDownload',
                type: 'post',
                data: JSON.stringify({
                    "data": {
                        "phone": phoneNumber
                    }
                }),
                contentType: 'application/json',
                dataType: 'json',
                success: function (data) {
                    console.info(data);
                }
            });
          console.log(phoneNumber)
        }
        button.addEventListener("click", event => {
            console.log("clicked");
            sendSMS();
        });
</script>