I am really new to code like this. I have been getting an error that says
http://IP/test 405 (Method Not Allowed) jquery-3.6.4.min.js:2
Here is my Python code code:
from flask import Flask
import template.SQL as SQL
import json
from flask import request
from flask import Flask, render_template
app = Flask(__name__)
app = Flask(__name__)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/test', methods=(['POST']))
def test():
print('anything')
output = request.getjson()
print(output)
print(type(output))
result = json.loads(output)
print(result)
print(type(result))
test = request.getjson
if __name__=="__main__":
app.run(debug=True)
And my HTML code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1 class="title">title</h1>
<h2>Title</h2>
<label for="fname">First Name:</label><input type="text" name="fname" id="fname">
<label for="lname">Last Name:</label><input type="text" name="lname" id="lname">
<button type="submit" onclick='myFunction();'>Enter</button>
<script
src="https://code.jquery.com/jquery-3.6.4.min.js"
integrity="sha256-oP6HI9z1XaZNBrJURtCoUT5SUnxFr8s3BzRl+cbzUq8="
crossorigin="anonymous"></script>
<script>
function myFunction() {
const firstname = document.getElementById('fname').value;
const lastname = document.getElementById('lname').value;
const dict_values = {firstname, lastname}
const s = JSON.stringify(dict_values);
console.log(s)
window.alert(s)
$.ajax({
url:"/test",
type:"POST",
contentType: "application/json",
data: JSON.stringify(s)});
}
</script>
</body>
</html>
I have no idea why the port isn't working. I'm running it on VS Code.
I've tried googling, going line by line, and taking out portions of code... but nothing helped me come closer to fixing it.