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);
});
});
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);
if(!formValid(data)) {
event.preventDefault(); // отменяем action формы
return;
}
})
document.getElementById("myForm").addEventListener('submit', (event) => {
const data = new FormData(event.target);
fetch("/register", {
method: 'POST',
body: data
})
.then((response) => response.json())
.then((data) => {
// тут рисуешь алерты в DOM
})
.catch((error) => {
console.log(`fetch.post response came up with an error: ${error}`);
});
event.preventDefault();
})
if request.method == 'POST':
data = request.form
ok, err = validateForm(data)
if not ok:
return jsonify(err)
DoRegisterUser(data)
return redirect(url_for(index))
<div>
<button id="jsfetch">fetch json</button>
</div>
document.querySelector("#jsfetch").addEventListener("click", Handler);
function Handler(event) {
fetch('/api')
.then((response) => {
return response.json();
})
.then((myjson) => {
console.log(myjson);
});
}
from flask import jsonify
data = {
"id": 123,
"name": "Вася",
"surname": "Пупкин"
}
@app.route('/api')
def api():
return jsonify(data)
Object { id: 123, name: "Вася", surname: "Пупкин" }
function LetterChanges(str) {
const alphabet = 'abcdefjhijklmnopqrstuvwxyz_'
const vowel = 'aeiou'
return str.toLowerCase().split('').map((c) => {
c = alphabet[alphabet.indexOf(c)+1];
if (vowel.includes(c)) c = c.toUpperCase();
if (c === "_") c = "A"
console.log(c);
return c;
}).join('');
}