如何从 svelte Twilio API 中删除 CORS 错误?
我的目标是在我的应用程序中发送文本。我在 netlify 上托管过,但也在 localhost:5000 上托管过,但都不起作用。我正在使用 svelte 和 twilio。我收到以下错误:
可以从“https://svelte-service-xxxx.twl.io/sms”获取数据 来源“https://xxxxxxx.netlify.app”已被阻止 CORS 策略: 上不存在“Access-Control-Allow-Origin”标头 请求的资源。如果不透明的响应满足您的需求,请设置 请求的模式为“no-cors”以在禁用 CORS 的情况下获取资源。
我在教程中使用无服务器功能并复制了所有代码。我在上面找不到任何东西?我该如何解决 CORS 问题?
教程:https://www.twilio.com/blog/send -sms-svelte-twilio-功能 尝试过这个:没有“访问权限” -Control-Allow-Origin' 标头出现在请求的资源上 - 当尝试从 REST API 获取数据时
MYSVELTE
<script>
const handleText = async () => {
const response = await fetch("https://svelte-service-xxxx.twil.io/sms");
const data = await response.json();
console.log(data);
status = data.status;
};
</script>
MY FUNCTION IN TWILIO(根据教程)
export async function handler(context, event, callback) {
const twilioClient = context.getTwilioClient();
const messageDetails = {
body: "Ahoy, Svelte developer!",
to: "+xxxxxxxxxx",
from: "+xxxxxxxxxx"
}
const response = new Twilio.Response();
const headers = {
"Access-Control-Allow-Origin": "https://xxxxx.netlify.app/",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type", <<----?????
"Content-Type": "application/json"
};
response.setHeaders(headers);
try {
const message = await twilioClient.messages.create(messageDetails);
response.setBody({
status: `Your message was sent successfully with SID: ${message.sid}`
});
return callback(null, response);
} catch(error) {
response.setBody({
status: error
});
return callback(response);
}
}
My goal is to send a text in my app. I have hosted at netlify, but also had on localhost:5000, neither worked. I am using svelte and twilio. I am getting the following error:
Access to fetch at 'https://svelte-service-xxxx.twil.io/sms' from
origin 'https://xxxxxxx.netlify.app' has been blocked by
CORS policy: No 'Access-Control-Allow-Origin' header is present on the
requested resource. If an opaque response serves your needs, set the
request's mode to 'no-cors' to fetch the resource with CORS disabled.
I am using the serverless function in the tutorial and copied all that code. I can't find anything on it? How do i get around this CORS thing?
The tutorial: https://www.twilio.com/blog/send-sms-svelte-twilio-functions
Tried this:No 'Access-Control-Allow-Origin' header is present on the requested resource—when trying to get data from a REST API
MYSVELTE
<script>
const handleText = async () => {
const response = await fetch("https://svelte-service-xxxx.twil.io/sms");
const data = await response.json();
console.log(data);
status = data.status;
};
</script>
MY FUNCTION IN TWILIO (according to the tutorial)
export async function handler(context, event, callback) {
const twilioClient = context.getTwilioClient();
const messageDetails = {
body: "Ahoy, Svelte developer!",
to: "+xxxxxxxxxx",
from: "+xxxxxxxxxx"
}
const response = new Twilio.Response();
const headers = {
"Access-Control-Allow-Origin": "https://xxxxx.netlify.app/",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
"Access-Control-Allow-Headers": "Content-Type", <<----?????
"Content-Type": "application/json"
};
response.setHeaders(headers);
try {
const message = await twilioClient.messages.create(messageDetails);
response.setBody({
status: `Your message was sent successfully with SID: ${message.sid}`
});
return callback(null, response);
} catch(error) {
response.setBody({
status: error
});
return callback(response);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里有两个问题。首先,您更改了对 ES 模块的导出,目前 Twilio Functions 不支持该导出。所以你有:
但应该是:
你首先在这里看到了 CORS 问题,因为 Twilio 函数损坏时的响应不包含 CORS 标头。但是,您仍然遇到 CORS 问题。
当浏览器发送
Origin
标头时,它的尾部没有斜杠。该Origin
必须与Access-Control-Allow-Origin
标头中的 URL 匹配。因此,您需要更改标题以删除尾部斜杠:There are two issues here. First up, you changed the export to ES modules, which is currently unsupported by Twilio Functions. So you have:
but it should be:
You were seeing a CORS issue here to start with because the response from a Twilio Function when it is broken does not include CORS headers. However, you do still have a CORS issue.
When a browser sends the
Origin
header, it does so without a trailing slash. ThatOrigin
has to match the URL in theAccess-Control-Allow-Origin
header. So you need to change the header to drop the trailing slash: