name = request.form["username"]
<input type="text" name = "username" blablabla >
from fastapi import FastAPI
app = FastAPI()
@app.get("/dialog/")
def read_item(message: str, lang: str = 'ru'):
return {'message': detect_intent_texts(TOKEN, SESSION_ID, message, lang)
document.querySelector("#button").addEventListener("click", (event) => {
const msg = document.querySelector("#message").value;
const lang = document.querySelector("#lang").value;
fetch('/dialog/?' + new URLSearchParams({
message: msg,
lang: lang,
}))
.then((response) => {
if (!response.ok) {
throw new Error("HTTP error, status = " + response.status);
}
return response.json();
})
.then((data) => {
alert(data.message);
})
.catch((error) => {
alert(error);
});
});
return Response("This is your login", status=200, content_type="text/plain")
return jsonify({"message": "This is your login"}), 200
const formData = new FormData();
const photos = document.querySelector('input[type="file"][multiple]');
formData.append('title', 'My Vegas Vacation');
for (let i = 0; i < photos.files.length; i++) {
formData.append('photos', photos.files[i]);
}
fetch('https://example.com/posts', {
method: 'POST',
body: formData,
})
.then(response => response.json())
.then(result => {
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
Макросы раскрываются в требуемое значение в зависимости от контекста
document.getElementById("myForm").addEventListener('submit', (event) => {
const data = new FormData(event.target);
data.append('formname','myform');
fetch("/formproc", {
method: 'POST',
body: data
})
.then((response) => response.text())
.then((data) => {
console.log(data);
})
.catch((error) => {
console.log(`error: ${error}`);
});
event.preventDefault(); // если не нужно перегружать страницу
})