-1

I try to send a POST request as I change the select box for my page running on local host.

Here are my codes;

This is my Flask function that shoudl respond to POST

@app.route('/chng_clf', methods=['POST', 'GET'])
def change_clf():
    clf_name = flask.request.args.get('clf_name', '')
    print clf_name

This is the Jquery code doing AJAX in

    <script type="text/javascript">
        $("#select_clf").on("change",function() {
             $.ajax({  
                        type: "GET"
                        url: 'chng_clf',
                        data: { 
                        'clf_name': $(this).val(), 
                        },
                        success: function(msg){
                            alert('wow' + msg);
                        }
                        error: function(msg){
                            alert('wow!!' + msg);
                        }
             });
        });
    </script>

This is the select box in

<select id="select_clf">
          <option value="scene">Scene Model</option>
          <option value="object">Object Model</option>
          <option value="clothes">Clothes Model</option>
          <option value="concept">Concept Model </option>
</select>

However for some reason I cannot get any change after I change the select box. Could you point the problem about this code?

erogol
  • 13,156
  • 33
  • 101
  • 155
  • 2
    Maybe you need change `type: "GET"` to `type: "POST"` ? – Oleksandr T. Dec 07 '15 at 11:10
  • If GET is used then data could be passed as query string parameters. For this modifications to the server method has to be taken care of for parsing those parameters. http://stackoverflow.com/questions/10298899/how-to-send-data-in-request-body-with-a-get-when-using-jquery-ajax – akgaur Dec 07 '15 at 11:15
  • I tried both GET and POST but still same – erogol Dec 07 '15 at 12:45
  • Is the JavaScript added to the page after the `select`? If not, it runs before the `select` exists. – dirn Dec 07 '15 at 13:12

2 Answers2

0

try adding cache:false as a property on the ajax object - it might be that the browser is caching the request/response to your server

Rob Wilson
  • 1,123
  • 1
  • 8
  • 13
0

The problem is being a noob basically. I added the $(document).ready(function(){...}); covering my js then everything is fine now.

erogol
  • 13,156
  • 33
  • 101
  • 155