WebUI: clean up fetch API usage

The `Content-type` header isn't required since `URLSearchParams` is present.
The `method` property is preferred to be always specified for clarity.
The `cache: "no-store"` is preferred for most GET requests to avoid caching.

PR #21891.
This commit is contained in:
Chocobo1 2024-11-25 13:49:35 +08:00 committed by GitHub
parent f022ce8f84
commit 8d847eeb18
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 11 additions and 11 deletions

View file

@ -278,7 +278,7 @@ window.qBittorrent.Misc ??= (() => {
const downloadFile = async (url, defaultFileName, errorMessage = "QBT_TR(Unable to download file)QBT_TR[CONTEXT=HttpServer]") => { const downloadFile = async (url, defaultFileName, errorMessage = "QBT_TR(Unable to download file)QBT_TR[CONTEXT=HttpServer]") => {
try { try {
const response = await fetch(url); const response = await fetch(url, { method: "GET" });
if (!response.ok) { if (!response.ok) {
alert(errorMessage); alert(errorMessage);
return; return;

View file

@ -64,7 +64,10 @@ window.qBittorrent.pathAutofill ??= (() => {
if (partialPath === "") if (partialPath === "")
return; return;
fetch(`api/v2/app/getDirectoryContent?dirPath=${partialPath}&mode=${mode}`) fetch(`api/v2/app/getDirectoryContent?dirPath=${partialPath}&mode=${mode}`, {
method: "GET",
cache: "no-store"
})
.then(response => response.json()) .then(response => response.json())
.then(filesList => { showInputSuggestions(element, filesList); }) .then(filesList => { showInputSuggestions(element, filesList); })
.catch(error => {}); .catch(error => {});

View file

@ -37,17 +37,12 @@ const submitLoginForm = (event) => {
const usernameElement = document.getElementById("username"); const usernameElement = document.getElementById("username");
const passwordElement = document.getElementById("password"); const passwordElement = document.getElementById("password");
const query = new URLSearchParams();
query.set("username", usernameElement.value);
query.set("password", passwordElement.value);
passwordElement.value = ""; // clear previous value
fetch("api/v2/auth/login", { fetch("api/v2/auth/login", {
method: "POST", method: "POST",
headers: { body: new URLSearchParams({
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8" "username": usernameElement.value,
}, "password": passwordElement.value
body: query.toString() })
}) })
.then(async (response) => { .then(async (response) => {
const responseText = await response.text(); const responseText = await response.text();
@ -62,6 +57,8 @@ const submitLoginForm = (event) => {
(error) => { (error) => {
errorMsgElement.textContent = `QBT_TR(Unable to log in, server is probably unreachable.)QBT_TR[CONTEXT=Login]\n${error}`; errorMsgElement.textContent = `QBT_TR(Unable to log in, server is probably unreachable.)QBT_TR[CONTEXT=Login]\n${error}`;
}); });
passwordElement.value = ""; // clear previous value
}; };
document.addEventListener("DOMContentLoaded", () => { document.addEventListener("DOMContentLoaded", () => {