1

I created a json object in jquery and sent it via ajax as a post request. I logged both the arrays and it has the correct format.

$('#submittt').on('click', function () {
    var names = [];
    var times = [];

    $(".input-medium.name").each(function() {
        names.push($(this).val());
    });

   $(".input-medium.time").each(function() {
        times.push(parseInt($(this).val()));
   });

  console.log(JSON.stringify(names));
  console.log(JSON.stringify(times));

  var data = {"names":names, "times":times};
  console.log(data);
     $.ajax({
         url: "/step/" + vid,
         type: "POST",
         data: 'data=' + JSON.stringify(data),
         dataType: "json",
     });

Now I am trying to get the json object in python (I am using GAE) and access its 2 arrays. I believe I am supposed to do something like

json_raw = self.request.get('data')
jsonObj = json.loads(json_raw)
namelist = jsonObj[0]['names']
print namelist

But this isn't working, it says it can't decode the json object. Anyone know what I am doing wrong?

2
  • It would be helpful if you posted (1) the JSON string, (2) the error you get and (3) where the error occurs in your code. Commented May 24, 2013 at 8:36
  • And you should log what your Python is getting for the value of json_raw. Commented May 24, 2013 at 8:55

1 Answer 1

1

In your JavaScript, instead of

data: 'data=' + JSON.stringify(data),

do

data: JSON.stringify(data),

And in your Python remove the [0] when retrieving namelist.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.