首页 > 解决方案 > 在 wixFormSubmit() 上发出 POST 请求

问题描述

我有一个配置为发送包含表单数据的电子邮件并仅在页面上显示确认消息的 Wix 表单。我被要求添加第二个请求以将相同的表单数据发送到不同的服务器。我能够使用 onWixFormSubmited() 将数据发送到后端

export function wixForms1_wixFormSubmitted() {
    let data = {
            first_name: $w('#input5').value,
            last_name: $w('#input4').value,
            email: $w('#input3').value,
    }

    saveProspectToCRM(data)
            .then(function(response) {
            console.log('it was successfull');
            console.log(response);
    });
}

import {fetch} from 'wix-fetch';  

export async function saveProspectToCRM(params) {
    const url = 'myurl';
    let  data = {
        //encoded data
    };
    
    return fetch(url,{
        method: 'post',
            body: data,
            headers: {
                    'Content-Type': 'application/x-www-form-urlencoded'
            }).then(function(response) {
                if (response.status >= 200 && response.status < 300) {
                    return response.text();
                } else {
                        throw new Error(response.statusText);
                }
    }); 

}

这实际上有效,正在发送电子邮件并将数据发布到我的另一台服务器,但我有第二个 Wix 表单,唯一的区别是提交后重定向到不同的页面,而不是只显示一条消息。而且我的代码不适用于第二种形式。我尝试将 onWixFormSubmitted 更改为 onWixFormSubmit 但仍然无法正常工作。

我在文档中读到 onWixFormSubmit只处理同步操作,我假设第一个表单有效,因为用户没有被重定向到不同的页面。任何人都可以告诉我我做错了什么,或者保持表单配置(发送电子邮件)并将数据发布到服务器的最佳方法是什么?

标签: javascriptnode.jshttprequestvelo

解决方案


wixFormSubmitted()具有重定向返回功能,一旦提交。其余代码被忽略。

您应该做的是切换回成功消息并将其隐藏。

对于重定向,您始终可以在 API 响应后使用wix-location 。

import wixLocation from 'wix-location';

// ...

wixLocation.to("/about-me"); 

参考


推荐阅读