从 svelte 应用程序发送文本到 twilio 功能
仍然无法弄清楚这个 Cors 的事情它很烦人。尝试测试从输入发送文本到 twilio。我想从手机向动态号码发送短信。无论我做什么,我总是收到 cors 错误?我肯定做错了什么,我不允许从本地主机发送吗?
SVELTE 应用程序
let phoneNumber = "";
const handleSendText = () => {
fetch("https://svelte-service-9106.twil.io/sms", {
method: "POST",
body: JSON.stringify({
to: phoneNumber,
from: "+11111111111",
}),
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("failed");
}
})
.catch((err) => {
console.log(err);
});
};
<input type="text" bind:value={phoneNumber} />
<button on:click={handleSendText}>Send</button>
TWILIO 功能
exports.handler = async function (context, event, callback){
const twilioClient = context.getTwilioClient();
const messageDetails = {
body: "Ahoy, Svelte developer!",
to: event.to,
from: event.from,
}
const response = new Twilio.Response();
const headers = {
"Access-Control-Allow-Origin" : "http://localhost:5000",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
"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);
}
}
Still can't figure out this Cors thing its pretty annoying. Trying to test sending a text from an input to twilio. I want to text from my phone to a dynamic number. I keep getting cors error no matter what i do? I am definitely doing something wrong, am i not allowed to send from local host?
SVELTE APP
let phoneNumber = "";
const handleSendText = () => {
fetch("https://svelte-service-9106.twil.io/sms", {
method: "POST",
body: JSON.stringify({
to: phoneNumber,
from: "+11111111111",
}),
headers: {
"Content-Type": "application/json",
},
})
.then((response) => {
if (!response.ok) {
throw new Error("failed");
}
})
.catch((err) => {
console.log(err);
});
};
<input type="text" bind:value={phoneNumber} />
<button on:click={handleSendText}>Send</button>
TWILIO FUNCTION
exports.handler = async function (context, event, callback){
const twilioClient = context.getTwilioClient();
const messageDetails = {
body: "Ahoy, Svelte developer!",
to: event.to,
from: event.from,
}
const response = new Twilio.Response();
const headers = {
"Access-Control-Allow-Origin" : "http://localhost:5000",
"Access-Control-Allow-Methods": "GET,PUT,POST,DELETE,OPTIONS",
"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)
我刚刚复制了您的函数,将其部署到我自己的 Twilio 帐户,并能够从
localhost:5000
向它发出成功的fetch
请求。但是,如果我因使用不正确的
from
号码等原因导致错误,则会收到 CORS 错误。问题是您的函数将响应对象作为回调函数的第一个参数返回。该参数保留用于错误对象,而不是响应,并且 Twilio Functions 在收到错误作为回调函数的第一个参数时将创建自己的响应。该响应不包含 CORS 标头,因此您会收到 CORS 错误。处理此问题的方法是发送您的响应并自行设置状态代码。
因此,将您的函数更新为:
您可以看到,在
catch
块中,我将状态代码设置为 400 并将响应对象作为回调的第二个参数返回。现在,您的前端代码也不会暴露您从中返回的错误,主要是因为当响应不正确时它会抛出自己的异常。尝试更新以解析错误正文,并使用您在错误中设置的
status
属性引发错误。一旦您像这样更新,您仍然会收到错误,但该错误将包含来自 API 的错误消息,您将能够修复该问题。
I just copied your Function, deployed it to my own Twilio account and was able to make successful
fetch
requests to it fromlocalhost:5000
.However, if I caused an error by, for example, using an incorrect
from
number, then I got the CORS error. The issue is that your Function is returning theresponse
object as the first argument to thecallback
function. That argument is reserved for error objects, not for responses, and Twilio Functions will create its own response when it receives an error as the first argument to the callback function. That response does not include CORS headers, so you get the CORS error.The way to deal with this is to send your response and set the status code on it yourself.
So, update your Function to:
You can see that in the
catch
block I am setting the status code to 400 and returning the response object as the second argument to the callback.Now, your front-end code is also not exposing the error that you would get back from this, mainly because it throws its own exception when the response is not correct. Try updating to parse the body of the error and throw an error using the
status
property that you set in the error.Once you update like this you will still get an error, but the error will contain the error message from the API and you will be able to fix that.