jquery – Why am I getting a 405 GET not allowed when trying to POST using flask?

I have a flask application running, where I have page for editing records in my python database. The address of this page is /edit/<id>. When an edit is submitted the entry is updated and the page redirects.

Therefore I wrote this AJAX route:

@app.route('/edit_entry', methods=['POST', GET])
def edit_entry():
    global data
    json_data = request.get_json()

    new_entry = {
        "id"   : json_data["id"],
        "title": json_data["title"],
        "image": json_data['image'],
    }

    data[json_data['id']] = new_entry
    return jsonify(id = new_entry['id'])

In my .js file I wrote

function edit_entry(new_entry) {
    $.ajax({
        type: "POST",
        url: "/edit_entry",
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: JSON.stringify(new_entry),
        success: function (result) {
        },
        error: function (request, status, error) {
            console.log("Error");
        }
    });
}

function mybuttonClicked() {
let new_entry =
        {
            "id"   :  $('#title').data("id"),
            "title": $('#title').val(),
            "image": $('#img').val(),
        }
        edit_entry(new_entry)
}

However I consistent get either GET 405 or PUT 405 messages, and I can’t remove the ‘GET’ specification without getting a 405 message.

Also I sporadically am told that "id" : json_data["id"], TypeError: 'NoneType' object is not subscriptable

Which I also don’t understand because in my html code I store my id value as a data-id in my title element, like so:

<input type="text" id="title" value="{{ item['title'] }}" data-id="{{ item ['id']}}">

Read more here: Source link