redirect
- URL페이지 이동
- 외부 내부 URL알면 redirect 할 수 있음
redirect("URL")
my_flask.py
from flask import Flask, request, redirect,url_for,jsonify
app = Flask(__name__)
@app.route('/')
def main():
return redirect("static/axios.html")
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
ajax
my_flask.py
from flask import Flask, request, redirect,url_for,jsonify
app = Flask(__name__)
@app.route('/ajax' ,methods=['post'])
def ajax():
date=request.get_json()
print(date['menu'])
return jsonify(result = "success", result2= date)
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0'
jq01.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="jquery-3.7.1.js"></script>
<script type="text/javascript">
function myclick() {
var param = {
'menu':'짬뽕'
};
$.ajax({
type: 'POST',
url: '/ajax',
data: JSON.stringify(param),
dataType : 'JSON',
contentType: "application/json",
success: function(resp){
console.log(resp)
console.log(param.menu)
}
})
}
$(document).ready(function(){
// 제이쿼리
$("#btn").on("click", function(event) {
myclick();
});
});
</script>
</head>
<body>
JQ01
<br>
<span id="my_span">Good Morning</span>
<input type="button" value="click" id="btn">
</body>
</html>
axios
my_flask.py
request.get_json() => axios.html 보낸 파라미터 받음
jsonify(message="ok") => axios.html 로 보낼 응답값
from flask import Flask, request, redirect,url_for,jsonify
app = Flask(__name__)
@app.route('/')
def main():
return redirect("static/axios.html")
@app.route('/axios' ,methods=['post'])
def axios():
data=request.get_json()
print(data['menu'])
return jsonify(message="ok")
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
axios.html
axios.post("url",url에 보낼 파라미터).then((url에서 받은 응답값)=>{
})
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="jquery-3.7.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script type="text/javascript">
function myclick() {
var param = {
'menu':'짬뽕'
};
axios.post("/axios",param).then((res) => {
console.log(res.data);
});
}
</script>
</head>
<body>
axios
<br>
<input type="button" value="click" onclick="myclick()">
</body>
</html>
원본
my_flask.py
from flask import Flask, request, redirect,url_for,jsonify
app = Flask(__name__)
@app.route('/')
def main():
return redirect("static/axios.html")
@app.route('/axios' ,methods=['post'])
def axios():
data=request.get_json()
print(data['menu'])
return jsonify(message="ok")
if __name__ == '__main__':
app.run(debug=True, port=80, host='0.0.0.0')
axios.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="jquery-3.7.1.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script type="text/javascript">
function myclick() {
var param = {
'menu':'짬뽕'
};
axios.post("/axios",param).then((res) => {
console.log(res.data);
});
}
</script>
</head>
<body>
axios
<br>
<span id="my_span">Good Morning</span>
<input type="button" value="click" onclick="myclick()">
</body>
</html>
'Python > flask' 카테고리의 다른 글
axios 이용한 CRUD (0) | 2024.04.10 |
---|---|
dao 이용한 select,insert,update,delete문 (0) | 2024.04.09 |
flask Insert,Update, Delete 문 (0) | 2024.04.03 |
render_template (0) | 2024.04.03 |
flask get방식, post 방식 파라미터 넣기 (0) | 2024.04.03 |