Ajax data element is array causing Python Unexpected query string parameters

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Ajax data element is array causing Python Unexpected query string parameters



I have an Ajax call to a backend Python program with this calling sequence:


var dataArray =

"company_id":company_id,
"plan_id":plan_id,
"demographics_to_save":demographics_to_save,
"takeup":takeup,
"number_of_employees_with_dependents":total_deps,
"percentMale":male_pct / 100,
"percentEmployeesWithDependents":deps_pct / 100


;

$.ajax(
url: "http://ec2.xx.xx.xx.compute-1.amazonaws.com:8080/save_census_manual",
dataType: 'json',
async: false,
data: dataArray,
success: function(data)
company_id = data.company_id;
plan_id = data.plan_id;



// success
);



The variable: demographics_to_save is a nested array that is structured as seen in the Chrome Dev Tools Console:



dataArray
company_id: 84, plan_id: 61, demographics_to_save: Array(10), takeup: "0.68", number_of_employees_with_dependents: 2, …company_id: 84demographics_to_save: Array(10)0: Array(4)0: 21: 12: 13: 1length: 4__proto__: Array(0)1: (4) [0, 0, 0, 0]0: 01: 02: 03: 0length: 4__proto__: Array(0)2: (4) [0, 0, 0, 0]3: (4) [0, 0, 0, 0]4: (4) [0, 0, 0, 0]5: (4) [0, 0, 0, 0]6: (4) [0, 0, 0, 0]7: (4) [0, 0, 0, 0]0: 01: 02: 03: 0length: 4__proto__: Array(0)8: (4) [0, 0, 0, 0]9: (4) [0, 0, 0, 0]length: 10__proto__: Array(0)number_of_employees_with_dependents: 2percentEmployeesWithDependents: 0.67percentMale: 0.67plan_id: 61takeup: "0.68"proto: Object



When the Ajax call is invoked, the Python responds with:
404 Not Found
Unexpected query string parameters: demographics_to_save[3], demographics_to_save[1], demographics_to_save[2], demographics_to_save[7], demographics_to_save[8], demographics_to_save[6], demographics_to_save[0], demographics_to_save[9], demographics_to_save[5], demographics_to_save[4]



Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 60, in call
return self.callable(*self.args, **self.kwargs)
TypeError: save_census_manual() got an unexpected keyword argument 'demographics_to_save[2]'



During handling of the above exception, another exception occurred:



Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py", line 631, in respond
self._do_respond(path_info)
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cprequest.py", line 690, in _do_respond
response.body = self.handler()
File "/usr/local/lib/python3.5/dist-packages/cherrypy/lib/encoding.py", line 221, in call
self.body = self.oldhandler(*args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 66, in call
raise sys.exc_info()[1]
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 64, in call
test_callable_spec(self.callable, self.args, self.kwargs)
File "/usr/local/lib/python3.5/dist-packages/cherrypy/_cpdispatch.py", line 197, in test_callable_spec
raise cherrypy.HTTPError(404, message=message)
cherrypy._cperror.HTTPError: (404, 'Unexpected query string parameters: demographics_to_save[3], demographics_to_save[1], demographics_to_save[2], demographics_to_save[7], demographics_to_save[8], demographics_to_save[6], demographics_to_save[0], demographics_to_save[9], demographics_to_save[5], demographics_to_save[4]')



The Python code is a simple function
def save_census_manual(company_id,plan_id,demographics_to_save,takeup,number_of_employees_with_dependents,percentMale,percentEmployeesWithDependents):



some code



return



I'd like to know why all the unexpected query string parameter errors are occurring (see above).



I am using a Python Web Development Framework - CherryPy
When Cherrypy creates the call to the "offending" function, the call looks like this:
http://ec2-xx-xx-xx-xx.compute-1.amazonaws.com:8080/save_census_manual?company_id=84&plan_id=61&demographics_to_save=%5B0%5D%5B%5D=2&demographics_to_save%5B0%5D%5B%5D=1&demographics_to_save%5B0%5D%5B%5D=1&demographics_to_save%5B0%5D%5B%5D=1&demographics_to_save%5B1%5D%5B%5D=0&demographics_to_save%5B1%5D%5B%5D=0&demographics_to_save%5B1%5D%5B%5D=0&demographics_to_save%5B1%5D%5B%5D=0&demographics_to_save%5B2%5D%5B%5D=0&demographics_to_save%5B2%5D%5B%5D=0&demographics_to_save%5B2%5D%5B%5D=0&demographics_to_save%5B2%5D%5B%5D=0&demographics_to_save%5B3%5D%5B%5D=0&demographics_to_save%5B3%5D%5B%5D=0&demographics_to_save%5B3%5D%5B%5D=0&demographics_to_save%5B3%5D%5B%5D=0&demographics_to_save%5B4%5D%5B%5D=0&demographics_to_save%5B4%5D%5B%5D=0&demographics_to_save%5B4%5D%5B%5D=0&demographics_to_save%5B4%5D%5B%5D=0&demographics_to_save%5B5%5D%5B%5D=0&demographics_to_save%5B5%5D%5B%5D=0&demographics_to_save%5B5%5D%5B%5D=0&demographics_to_save%5B5%5D%5B%5D=0&demographics_to_save%5B6%5D%5B%5D=0&demographics_to_save%5B6%5D%5B%5D=0&demographics_to_save%5B6%5D%5B%5D=0&demographics_to_save%5B6%5D%5B%5D=0&demographics_to_save%5B7%5D%5B%5D=0&demographics_to_save%5B7%5D%5B%5D=0&demographics_to_save%5B7%5D%5B%5D=0&demographics_to_save%5B7%5D%5B%5D=0&demographics_to_save%5B8%5D%5B%5D=0&demographics_to_save%5B8%5D%5B%5D=0&demographics_to_save%5B8%5D%5B%5D=0&demographics_to_save%5B8%5D%5B%5D=0&demographics_to_save%5B9%5D%5B%5D=0&demographics_to_save%5B9%5D%5B%5D=0&demographics_to_save%5B9%5D%5B%5D=0&demographics_to_save%5B9%5D%5B%5D=0&takeup=0.68&number_of_employees_with_dependents=2&percentMale=67&percentEmployeesWithDependents=67




1 Answer
1



Got around the issue by calling the function (via Ajax) with a change:
"demographics_to_save":JSON.stringify(demographics_to_save),



...then in the backend code (Python),
demographics_to_save = json.loads(demographics_to_save)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS