0

Below is my flask view:

import db_connect_test
from db_connect_test import Viz_Connector
from flask import Flask, request, session, redirect, url_for, render_template, flash,json,jsonify
import os
app = Flask(__name__)
@app.route('/',methods = ['GET','POST'])
def select_ID():
    if request.method == 'POST':
        ID=request.form['input_ID']
        Node_type = request.form['Node_Type']
        data = Viz_Connector(ID,Node_type).get_data()
        if data == 'Wrong ID and Node_type':
            return data
        else:
            return  json.dumps(data)
    return render_template('dropdown.html')

When data does not exist in the back end application database, the method:

       Viz_Connector(ID,Node_type).get_data() 

returns 'Wrong ID and Node_type'

Below is my ajax/query:

    $('input[type=submit]').click(function() {
  var input_ID = $("#input_ID").val();
  var Node_Type = $("#Node_Type").val();

  $.ajax({
        type: "POST",
        url: "/",
        dataType: 'json',

        data: {
          input_ID: input_ID,
          Node_Type: Node_Type,

        },
        success: function(data) {
            if (data == 'Wrong ID and Node_type') {
              alert(data);
            } else {
                var IDData = JSON.stringify(data); //--data getting passed from flask view when ID and node_type exist
                console.log(IDData);
                ---code to render graph---



                   }

       });
      return false;

     )}

When the ID and Node_type exist in the database everything works fine and the else part of both the Flask view and Ajax/jquery works and graphs get displayed.

When they are not found though, the alert piece does not work and no alert pops up.

Unable to figure out if anything is missing.

optimus_prime
  • 817
  • 2
  • 12
  • 34
  • Also, just FYI, because you are accessing the data in you're Flask view by `request.form` you can eliminate the `data` configuration in $.ajax() as Flask isn't seeing this. Ultimately if you want to pass JSON to Flask you'd need to use `data: JSON.stringify({...})` and `contentType: "application/json"` then access it in the view via `request.get_json()` but it seems like posting the form info works well for this case, but just FYI – abigperson Dec 22 '16 at 23:43
  • @PJSantoro I am indeed using JSON.stringify...forgot to mention that in the code above as I am only trying to make the alert work. Updated the post. – optimus_prime Dec 22 '16 at 23:50
  • Cool, I meant on the javascript data object you are sending...I don't think you'd want to use it on the response from Flask. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify – abigperson Dec 22 '16 at 23:55

1 Answers1

1

$.ajax() is expecting a JSON response (because of the dataType: 'json' option). try return jsonify(data) and you can eliminate the entire if data == 'Wrong ID and Node_type': conditional as it really isn't doing anything:

@app.route('/',methods = ['GET','POST'])
def select_ID():
    if request.method == 'POST':
        ID=request.form['input_ID']
        Node_type = request.form['Node_Type']
        data = Viz_Connector(ID,Node_type).get_data()
        return jsonify(data)
    return render_template('dropdown.html')

jsonify is a Response object, so it has content-type headers and the like, versus json.dumps which is just returning a valid JSON string. see this SO question for more info

Community
  • 1
  • 1
abigperson
  • 5,252
  • 3
  • 22
  • 25
  • I only need to alert when data returned from the application is "Wrong ID and Node_type" , so if I return jsonify(data), how would I differentiate what data it is in my ajax/jquery? So , that it alerts only it is "Wrong ID and Node_type" – optimus_prime Dec 22 '16 at 23:55
  • `return jsonify("Wrong ID and Node_type")` will show up as just a string in your AJAX. `jsonify` is just making it a valid HTTP JSON response otherwise your AJAX will never see it – abigperson Dec 23 '16 at 00:01
  • Sweet. Worked like a charm after using jsonify. Thanks a ton. – optimus_prime Dec 23 '16 at 00:08