I have a Javascript variable which is an array (I push strings into it) and I have to send that list back to the server through an AJAX jQuery.POST() method:
var user_list = new Array();
user_list.push("42");
var data = { user_list: JSON.stringify(user_list)};
$.post('/accounts/ajax/user_verification/', data)
.done( my_cb );
In the server, I am running Django and try to decode the POST request with the following view:
@login_required
def user_verification(request):
if not request.POST.has_key('user_list'):
print (__name__ + ', user_verification, raising BadRequest.')
raise Exception("'user_list' not found as a POST parameter.")
value = unicode(request.POST['user_list'])
print (__name__ + ', value, v = ' + value)
user_list = json.loads(request.body)
I get the following output and posterior exception thrown by " json.loads(request.body) ":
accounts.ajax, value, v = ["42"]
ValueError: No JSON object could be decoded
Isn't ["42"] a valid JSON array definition with a single element?
{"42"}instead of["42"]user_listin your JSON structure to something else. It might causing the problem. Also, to be on the safe side, can you change the JSON tovar data = { "newKey": JSON.stringify(user_list)}?