我看到很多檔案,但沒有 javascript。我缺乏知識在這里發揮作用,所以請原諒我。我想向輸入輸入的電話號碼發送一個簡單的文本。我設定了一個無服務器功能,但我不知道如何將電話號碼傳遞給它,除此之外我試圖向它寫一個發布請求,但我必須離開。我正在做的事情可能嗎?
let phoneNumber;
const handleSendText = () => {
fetch("https://svelte-service-xxxx.twil.io/sms", {
method: "post",
body: "This is a test",
headers: {
"Content-Type": "application/json",
},
to: JSON.stringify(phoneNumber),
from: " xxxxxxxxxxx",
})
.then((response) => {
if (!response.ok) {
throw new Error("failed");
}
})
.catch((err) => {
console.log(err);
});
};
<input type="text" placeholder="phone number" />
<button on:click={handleSendText}>Send</button>
uj5u.com熱心網友回復:
您需要將 的值系結<input>到腳本中的變數。
<script>
let phoneNumber = "";
const handleSendText = () => { // we'll come back to this };
</script>
<input type="text" placeholder="phone number" bind:value={phoneNumber} />
<button on:click={handleSendText}>Send</button>
這使得當您更改輸入中的值時,它會更新phoneNumber變數。
現在,在您的handleSendText函式中,您需要發送body請求中的所有引數。請注意,在以下代碼中,如何body包含您要在一個 JSON 字串化物件中發送的所有引數。
const handleSendText = () => {
fetch("https://svelte-service-9106.twil.io/sms", {
method: "POST",
body: JSON.stringify({
to: phoneNumber,
from: " 18035298025"
}),
headers: {
"Content-Type": "application/json",
}
})
.then((response) => {
if (!response.ok) {
throw new Error("failed");
} else {
console.log("Success!");
}
})
.catch((err) => {
console.log(err);
});
};
然后,在您的 Twilio 函式中,您將能夠訪問您在event物件中發送的引數。在這種情況下,您將擁有event.to和event.from。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/448252.html
標籤:api 邮政 暮光之城 twilio-api
上一篇:端點的正確REST樣式?
