javascript - 在 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只处理同步操作,我假设第一个表单有效,因为用户没有被重定向到不同的页面。任何人都可以告诉我我做错了什么,或者保持表单配置(发送电子邮件)并将数据发布到服务器的最佳方法是什么?
解决方案
wixFormSubmitted()具有重定向返回功能,一旦提交。其余代码被忽略。
您应该做的是切换回成功消息并将其隐藏。
对于重定向,您始终可以在 API 响应后使用wix-location 。
import wixLocation from 'wix-location';
// ...
wixLocation.to("/about-me");
推荐阅读
- c++ - 有没有办法确定程序是否将 -ggdb 作为 gcc 选项提供?
- reactjs - `react-map-gl` 错误模块未找到错误 [MAPBOX]
- c# - 如何使用 EF Core MongoDB 驱动程序比较表达式中的字符串和日期时间?
- javascript - 找不到容器dom元素融合图
- spring-boot - 对于未经身份验证的请求,Keyclock springboot 返回 403 而不是 401
- node.js - 为什么有些 URL 后面没有“follow-redirects”npm 包?
- cucumber - JIRA 的 XRAY “错误”:“customfield_11714:customfield_11714”
- javascript - 我如何在本机反应中多次使用组件
- android - 在 LazyColumn 中是否有任何正确(有效)的方式来使用 CustomView(有或没有 androidx.compose.ui.viewinterop.AndroidView)?
- c++ - Eclipse 调试 C++ 在 MacOS 上不起作用