0

I am new to AJAX and Flask. I asked here about how to pass values and show them on the same page without the need to refresh the page and I was told about Ajax. However, after modifying the example given. I am getting a weird error.

Here's my HTML side:

 <script type=text/javascript>
    $(function() {
      $('a#show').bind('click', function() {
        $.getJSON('/show', {
          a: $('input[name="node"]').val(),
          b: $('input[name="parameter"]').val()
        }, function(data) {
          $("#result").text(data.result);
        });
        return false;
      });
    });
  </script>

<form  action=""> <!-- method = 'post'-->
                <label for="node">Node</label>
                <select name="node">
                    {% for o in data_nodes %}
                    <option value="{{ o.name }}">{{ o.name }}</option>
                    {% endfor %}
                </select>
                <label for="parameter">Parameter</label>
                <select name="parameter">
                    {% for o in data_para %}
                    <option value="{{ o.name }}">{{ o.name }}</option>
                    {% endfor %}
                </select>
                <!-- Write your comments here -->
                <button type="submit" class="btn btn-default" href="javascript:void(0);" id="show" >Send</button>
</form>

My Flask side:

@app.route('/dashboard/', methods=['GET', 'POST'])
def dashboard():
    return render_template("dashboard.html", data_nodes = [{'name': 'c9 - Office'}, {'name': 'f20 - Home'}, {'name': 'f21 - School'}],
                           data_para = [{'name': 'Temperature'}, {'name': 'RSSI'}, {'name': 'LQI'}])  # data_nodes, data_para
@app.route('/show')
def show():
    a = request.form.get('node')
    b = request.form.get('parameter')
    print a
    print b
    result = a+b
    return jsonify(result) # I only intend to print the result on the page here for now. 

And here's the response I get. The page refreshes too.

127.0.0.1 - - [24/Apr/2016 23:41:10] "GET /dashboard/?node=f20+-+Home&parameter=RSSI&time=t3 HTTP/1.1" 200 
127.0.0.1 - - [24/Apr/2016 23:41:05] "GET /static/js/ie10-viewport-bug-workaround.js HTTP/1.1" 404 -

I tried different variations but I am not exactly sure why the result isn't appearing.

Community
  • 1
  • 1
Ahmed Al-haddad
  • 805
  • 2
  • 16
  • 41

1 Answers1

2

Based on your log file, you are making the following request to your webserver:

127.0.0.1 - - [24/Apr/2016 23:41:10] "GET /dashboard/?node=f20+-+Home&parameter=RSSI&time=t3 HTTP/1.1" 200

This means, the parameters you are looking for are coming in as request parameters, not as form elements. You can reference this post for a more thorough discussion, but I would just change form to args.

@app.route('/show')
def show():
    a = request.args.get('node')
    b = request.args.get('parameter')

Update:

It looks like there are also errors in your HTML file:

<script type=text/javascript>
    $(function() {
      $('#myForm').submit(function(ev) {
        $.getJSON('/show', {
          node: $('input[name="node"]').val(),
          parameter: $('input[name="parameter"]').val()
        }, function(data) {
          $("#result").text(data.result);
        });
        ev.preventDefault();
      });
    });
  </script>

<form id='myForm' action=""> <!-- method = 'post'-->
                <label for="node">Node</label>
                <select name="node">
                    {% for o in data_nodes %}
                    <option value="{{ o.name }}">{{ o.name }}</option>
                    {% endfor %}
                </select>
                <label for="parameter">Parameter</label>
                <select name="parameter">
                    {% for o in data_para %}
                    <option value="{{ o.name }}">{{ o.name }}</option>
                    {% endfor %}
                </select>
                <!-- Write your comments here -->
                <button type="submit" class="btn btn-default" href="javascript:void(0);" id="show" >Send</button>
</form>
Community
  • 1
  • 1
2ps
  • 15,099
  • 2
  • 27
  • 47