Home > Enterprise >  Bottle.py | request.forms.get() returning NoneType using AJAX
Bottle.py | request.forms.get() returning NoneType using AJAX

Time:05-07

I am trying to send javaScript information into a bottle.py server using AJAX. After trying nearly every solution I could find on stackOverflow, bottle docs, or Google in general. No solutions have worked.

For clarity sake, I have only included the parts of the code pertaining to the AJAX call. Can anyone explain why request.forms.get is returning None, and how to correct that?

This is the JS code:

let rawData = {"user0Name": users[0],
     "user0Bucks": userBetaBucks[0],
     "user1Name": users[1],
     "user1Bucks": userBetaBucks[1],
     "user2Name": users[2],
     "user2Bucks": userBetaBucks[2],
     "user3Name": users[3],
     "user3Bucks": userBetaBucks[3],
     "user4Name": users[4],
     "user4Bucks": userBetaBucks[4]
    };

$.ajax({
    url: "/updateValues",
    type: "POST",
    dataType: "json",
    data: rawData,
    contentType: "application/json;",
});

This is the python code:

@post('/updateValues')
    def updateValues():
    session = load_session(request)
    gameID = session['gameID']
    rawInfo = json.load(request.forms.get('data'))

Note: 'session = load_session(request)' is a custom function created for retrieving cookies and loading them for later modification within this function. Such as the gameID you see here.

CodePudding user response:

Turns out I needed to be using JSON.stringify() on the data. All is fixed.

  • Related