如何在 Javascript / Typescript 中使用 Storyblok 电子邮件服务?

How can I use the Storyblok Email Service in Javascript / Typescript?

我已经包含了 storyblok 对象和 serializeArray 函数:

import { serializeArray } from '../utils';
import { storyblok } from '../shims/Storyblok';

现在我想将表单数据传递给 sendEmail 函数。它是如何工作的?

submit(event:Event) {
    event.preventDefault();
    let formData = serializeArray(<HTMLFormElement> this.$());

    // what to do here?

    storyblok.sendEmail(message,
        // success
        function(data:any) {
            console.log(data);
            console.log('success');
        },
        // error
        function(data:any) {
            console.log(data);
            console.log('error');
        }
    )
}

Storyblok 联系表 API 允许您按以下格式传递参数:

var message = {};
message['message[email]'] = 'Valid Email goes here'; // assign your email field here
message['message[body]']  = 'This is my Message'; // assign your message field here

这 2 个字段 (emailbody) 是必需的 - 您可以将任意数量的参数传递给 API。

您可以使用以下 cURL 请求直接尝试您的请求

curl 'https://api.storyblok.com/v1/messages?token={{YOUR_TOKEN}}' 
-H 'pragma: no-cache' 
-H 'origin: {{YOUR_SPACE_DOMAIN}}' 
-H 'accept-encoding: gzip, deflate, br' 
-H 'x-requested-with: XMLHttpRequest' 
-H 'accept-language: de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4,hr;q=0.2,el;q=0.2,bg;q=0.2,sr;q=0.2,fr;q=0.2,sq;q=0.2' 
-H 'user-agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' 
-H 'content-type: application/x-www-form-urlencoded' 
-H 'accept: */*' 
-H 'cache-control: no-cache' 
-H 'authority: api.storyblok.com' 
-H 'referer: {{YOUR_SPACE_DOMAIN}}' --data 'message%5Bemail%5D=test%40test.test&message%5Bbody%5D=yourMessageGoesHere' --compressed