goelakash
goelakash

Reputation: 2519

405 : Method not allowed (AJAX query to Flask)

I have seen a lot of questions on this and I am not able to debug this.

My ajax query is this:

    function add_to_pipeline(){
        // console.log("In function");
        var mySelect = document.getElementById("select_list");
        var operator = mySelect.options[ mySelect.selectedIndex ].label;
        // console.log(mySelect.options[ mySelect.selectedIndex ].value);
        $("#pipeline_list").append('<li> '+ operator +'</li>');
        submit_to_server();
    }

    function submit_to_server(){
        var img = new FormData();
        img.append('file',$("#preview_list li:last-child")[0]);

        // Debugging statement
        //$("#preview_list li:last-child").dialog();
            $.ajax({
            type : "POST",
            url : "/_process",
            data : img,
            contentType: false,
            cache: false,
            processData: false,
            success: function(data) {
                alert(data['result']);
            }
        });
    }

The HTML is:

   <select id="select_list">
             {% for l in list %}
                 <option value={{ l.index }}>{{ l.name }}</option>
             {% endfor %}
   </select>
   <input type="button" id="p_submit" value="Add to pipeline" onclick="add_to_pipeline()">

The Flask file:

@app.route('/_process')
def process():
    img = request.files['file']
    return jsonify(result="Image received")

I am taking the last <li> element of a <ul> as input file (images) and sending it through AJAX.

Upvotes: 3

Views: 4417

Answers (1)

Andrii Rusanov
Andrii Rusanov

Reputation: 4606

You need to specify methods allowed in your view(default is GET), for example:

@app.route('/_process', methods=['GET', 'POST'])

I think in your case you don't need a GET, so simply:

@app.route('/_process', methods=['POST'])
def process():
    img = request.files['file']
    return jsonify(result="Image received")

Here is example in the docs: http://flask.pocoo.org/docs/0.10/quickstart/#http-methods

Upvotes: 4

Related Questions