0

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.

M Z
  • 4,571
  • 2
  • 13
  • 27
Burrito
  • 11
  • 2
  • Welcome to Stack Overflow. Why do you have ```methods=(["POST"])```? – ewokx Apr 27 '23 at 03:28
  • 1
    Thanks. And for the answer, is there not supposed to be a methods=(["POST"])? – Burrito Apr 27 '23 at 04:35
  • @ewokx it's there to denote the endpoint only accepts HTTP POST requests – M Z Apr 27 '23 at 05:05
  • @MZ I meant the ```()``` in ```(["POST"])```. But isn't there supposed to be an actual url instead of just ```/test```? – ewokx Apr 27 '23 at 05:22
  • @ewokx I don't think so? What do you mean by actual URL? The domain is just localhost here. The first argument provided with `/test` is the endpoint, that's just Flask logic iirc – M Z Apr 27 '23 at 05:24
  • @Burrito I don't run into any issues running your code. Can you provide the exact action you're doing to cause the `405` message? I can reach the page and create a post request properly. And since it isn't technically CORS, https://stackoverflow.com/q/17333013 might not be useful. – M Z Apr 27 '23 at 05:31
  • @Burrito Without knowing more, the only thing I'd suggest trying is downloading the jquery.js file and store it locally. This ensures no CORS shenanigans. Also add a proper return to your post function because it currently throws an error – M Z Apr 27 '23 at 05:32
  • @MZ I meant in the ajax call. – ewokx Apr 27 '23 at 05:40
  • @ewokx you can give relative paths to ajax calls – M Z Apr 27 '23 at 05:43

1 Answers1

0

Your Flask route isn't registered properly, it should be the following instead (without the additional bracket, else it will be treated as tuples):

@app.route('/test', methods=['POST'])
AngYC
  • 3,051
  • 6
  • 20
  • I'm still getting the same error, even after changing it. – Burrito Apr 27 '23 at 04:07
  • 1
    The additional bracket producing tuples is completely not true. Adding parentheses to a single item does not produce a tuple. You can easily test this. For example, `(((((['POST'])))))` is still the same as `['POST']`. To produce a tuple with a single item, you need to provide a trailing comma. Like `(['POST'],)` – M Z Apr 27 '23 at 05:07
  • Hi @MZ, oops that's my mistake, sorry for the confusion. Do you know any other reason why the original poster is receiving 405 error? – AngYC Apr 27 '23 at 05:16