Example Get and Post requests.¶
The redirect must be turned on in the ajax request.
Caution
If you get a CORS error while posting, set the Content-Type
to text/plain
.
Add¶
fetch("https://script.google.com/.../exec?action=add&data=Hello,World")
.then(res => res.json())
.then(res => console.log(res.status + ", id: " + res.id));
fetch("https://script.google.com/.../exec?action=add", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
data: ["Hello", "World"]
})
})
.then(res => res.json())
.then(res => console.log(res.status + ", id: " + res.id));
Delete¶
fetch("https://script.google.com/.../exec?action=delete&id=123456")
.then(res => res.json())
.then(res => console.log(res.status));
fetch("https://script.google.com/.../exec?action=delete", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
id: "123456"
})
})
.then(res => res.json())
.then(res => console.log(res));
Read¶
fetch("https://script.google.com/.../exec?action=read&id=123456")
.then(res => res.json())
.then(res => console.log(res)); // { status: true, data: ["Hello", "World"] }
fetch("https://script.google.com/.../exec?action=read", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
id: "123456"
})
})
.then(res => res.json())
.then(res => console.log(res));
List¶
Lists all data if there is no limit parameter.
fetch("https://script.google.com/.../exec?action=list&limit=3")
.then(res => res.json())
.then(res => console.log(res)); // { status: true, data: [["Hello", "World"], [...], [...]] }
fetch("https://script.google.com/.../exec?action=list", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
limit: 3
})
})
.then(res => res.json())
.then(res => console.log(res));
Update¶
fetch("https://script.google.com/.../exec?action=update&id=123456&data=Hi,World")
.then(res => res.json())
.then(res => console.log(res));
fetch("https://script.google.com/.../exec?action=update", {
method: "POST",
headers: {
"content-type": "application/json"
},
body: JSON.stringify({
id: "123456",
data: ["Hi", "World"]
})
})
.then(res => res.json())
.then(res => console.log(res.message + ", id: " + res.id));