Hi everyone, first of all let me thank you for creating such a useful tool!
I am currently having a bit of an issue trying to get xml files to run via ajax, I am trying to display R2LA data on our website and I want to get richer results when going through historical round information, I am aware of this code;
Code:
$.ajax({
url: 'http://your-r2la-address:port/report/get_report_jsonp',
type: 'GET',
dataType: 'jsonp',
crossDomain: true,
data: {'file_name': '2020_06_27_08_16_10-54R1.xml'},
success: function (data, textStatus, xhr) {
console.log(data);
},
error: function (xhr, textStatus, errorThrown) {
console.log(errorThrown);
}
});
But I can't get this to work at all, so much so that I've tried it on someone else's R2LA site and I get exactly the same error. Am I doing something wrong?
PS: After a bit of digging through the error folder, this is what everyone's favourite LLM came up with:
What’s going wrong
Your
report/get_report_jsonp action is building a Python dict that contains at least one
gluon.html.DIV (a Web2py HTML object). Then it calls json.dumps(...)
without a default=... converter. Python’s JSON encoder doesn’t know how to serialize a DIV, so you get:
TypeError: Object of type DIV is not JSON serializable
You can see in your trace it’s literally failing inside controllers.report.get_report_jsonp.py at the json.dumps(...) call, and Web2py even tries to render report/get_report_jsonp.html (an HTML view), which suggests the action falls back to a view path that injects a DIV into the dict it’s trying to dump.
Minimal, safe patch in the existing action
Edit
applications/r2la/controllers/report.py (the
source; Web2py will recompile), inside def get_report_jsonp() where you do the json.dumps. Change it to sanitize non-JSON objects:
Code:
import json
def _safe_default(o):
# Convert Web2py HTML helpers (DIV, SPAN, etc.) to strings
try:
return o.xml() # most web2py html objects have .xml()
except Exception:
return str(o)
# ... inside get_report_jsonp():
callback = request.vars.get('callback') or 'callback'
# whatever you currently build as `data`:
# data = {...}
payload = json.dumps(data, ensure_ascii=False, default=_safe_default)
return f'{callback}({payload});'