该表单在 amp 游乐场和 amp Gmail 游乐场中提交得非常好。当我向自己发送电子邮件并在 Gmail 中打开它时,动态内容会正确加载,但在提交表单时,我收到以下错误:
Uncaught (in promise) Error: Class$obf_1008: [https://dynamicmail-pa.googleapis.com/v2/xhrs:proxy?alt=protojson] Cg: Unsupported HTTP status: 400: Class$obf_1007: [object Object]
log.js:258 [amp-form] Form submission failed: Error: Request viewerRenderTemplate failed: Error: Class$obf_1008: [https://dynamicmail-pa.googleapis.com/v2/xhrs:proxy?alt=protojson] Cg: Unsupported HTTP status: 400: Class$obf_1007: [object Object]
我知道这可能是由于 CORS。在我的服务器上,我尝试"@ampproject/toolbox-cors"
按照官方文档的建议使用该库:
const app = express();
const ampCors = require("@ampproject/toolbox-cors");
const cors = require("cors");
app.use(
ampCors({
email: true,
verifyOrigin: false,
verbose: true
})
);
我还尝试像这样手动设置所有标题:
const whitelist = [
"https://playground.amp.dev",
"https://mail.google.com",
"https://amp.gmail.dev"
];
const corsOptions = {
origin: function (origin, callback) {
console.log("origin", origin);
if (!origin) {
return callback(null, true);
}
if (whitelist.indexOf(origin) !== -1) {
return callback(null, true);
} else {
return callback(new Error("Origin not in whitelist"));
}
}
};
app.use(cors(corsOptions));
app.use((req, res, next) => {
res.set("AMP-Access-Control-Allow-Source-Origin", "amp@gmail.dev"); //I've changed this to my sender email address when testing from Gmail
res.set(
"Access-Control-Expose-Headers",
"AMP-Access-Control-Allow-Source-Origin"
);
res.set("Access-Control-Allow-Credentials", true);
next();
});
如前所述,这两种方法在操场上都可以正常工作,但是从 Gmail 发布时表单仍然会导致相同的错误。