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.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Turns out I needed to be using JSON.stringify() on the data. All is fixed.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0