-1

I have a Flask web app that receives AJAX statuses/requests via request.args.get however one of my requests aren't being read by my Flask app. The other status status works fine, but doorStatus does not.

The data is successfully sent by AJAX since I can see the reqeust when I go to dev tools > network in my browser. Am I missing something?

from flask import Flask, request, jsonify
import serial
import time

app = Flask(__name__)

ser = serial.Serial('/dev/ttyACM0', 9600)

# {{url}}/led?status=on
@app.route('/', methods=['GET'])
def led():
    status = request.args.get('status')
    if status == "on":
        exteriorOn = 'H'
        exteriorOnEncode = exteriorOn.encode()
        ser.write(exteriorOnEncode)
        return jsonify({"message": "Led successfully turned on"})
    elif status == "off":
        exteriorOff = 'C'
        exteriorOffEncode = exteriorOff.encode()
        ser.write(exteriorOffEncode)
        return jsonify({"message": "Led successfully turned off"})
    else:
        return jsonify({"message": "Not a valid status"})


    doorStatus = request.args.get('doorStatus')
    if doorStatus == "open":
        doorOpen = 'L'
        doorOpenEncode = doorOpen.encode()
        ser.write(doorOpenEncode)
        return jsonify({"message": "Door successfully opened"})
    elif doorStatus == "closed":
        doorClosed = 'N'
        doorClosedEncode = doorClosed.encode()
        ser.write(doorClosedEncode)
        return jsonify({"message" : "Door successfulled closed"})
    else:
        return jsonify({"message" : "Not a valid status"})

JS

$(document).ready(function() {
$('#openDoor').on('click', function(e){
    let doorStatus;
    if($(this).text() == 'OPEN') {
    $(this).text('CLOSE')
    doorStatus = 'open';
   } else {

    $(this).text('OPEN');
    doorStatus = 'closed';
    }
    $.ajax({
        url: '/led',
        method: 'GET',
        data: {doorStatus},
        success: function(result) {
        console.log(result);
     }
    });
    e.preventDefault();
});

$('#exteriorBtn').on('click', function(e){
    let status;
    if($(this).text() == 'Exterior') {
        $(this).text('Turn Off')
        status = 'on';
    } else {
        $(this).text('Exterior');
        status = 'off';
    }

    $.ajax({
        url: '/led',
        method: 'GET',
        data:{status},
        success: function(result) {
            console.log(result);
     }
    });
    e.preventDefault();
    });
});
SpicyPat
  • 85
  • 1
  • 9
  • Your `data` is not formatted correctly. – Twisty Dec 22 '20 at 16:38
  • It's `data: {status},` right? The exact same works for my `status` ajax call, but not door status. Do you mean it's not consistent with the request in `request.args.get`? – SpicyPat Dec 22 '20 at 16:40

1 Answers1

0

When you GET or POST data, it needs to have a name and value pair.

GET Example:

http://www.nowhere.place/webpage.html?status=open

You can see the name status and it's value open.

See more: https://api.jquery.com/jquery.ajax/

data

Type: PlainObject or String or Array

Data to be sent to the server. If the HTTP method is one that cannot have an entity body, such as GET, the data is appended to the URL

Plain Object Example:

data: { status: "OPEN" }

String Example:

data: encodeURIComponent("status=OPEN")

Array Example:

data: [ "OPEN" ]

So for your code, I would advise:

$('#openDoor').on('click', function(e){
  e.preventDefault();
  var door = { status: "" };
  if($(this).text() == 'OPEN') {
    $(this).text('CLOSE');
    door.status = 'open';
  } else {
    $(this).text('OPEN');
    door.status = 'closed';
  }
  $.ajax({
    url: '/led',
    method: 'GET',
    data: door,
    success: function(result) {
      console.log(result);
    }
  });
});
Twisty
  • 30,304
  • 2
  • 26
  • 45
  • Thanks, What would the identified be to reference in `request.args.get`? Is it `status`? – SpicyPat Dec 22 '20 at 16:50
  • @SpicyPat I don't use Flask, but I suspect what you have is correct. Since you were not passing the variable correctly to Flask, I suspect your code was not finding the `status`. Take a look here: https://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request – Twisty Dec 22 '20 at 16:54
  • Looks like `request.args.get('status')` and `request.args['status']` should work. The `.get()` is used in case the index is missing, so I would stick with that. – Twisty Dec 22 '20 at 16:56