Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

removing `u` character in python output

I create a form and when I click the submit button, I assign the 3 value into a javascript dict and send it over to a python script to process however My web browser tell me a error!

from Json error: {u'food': 90, u'cargo': 70, u'fuel': 50} SyntaxError

controller.js

function customiseCtrl($xhr){
var self = this;

checkPoint();
this.process = function(){
    if (checkPoint()){

        var newPlayer = {"fuel":value, "food":value2, "cargo":value3 };

        $xhr('POST', '/process', newPlayer, function (code, response) {
            self.x = response;

        });
    }
};


}

/process --> python script (I am trying to read the information of "info" and write it into the Google app engine.

def post(self):
 user = users.get_current_user()
 player = Player();

 info = json.loads(self.request.body)
 player.fuel = info.fuel
 self.response.out.write(info)
like image 211
Brian Li Avatar asked Mar 15 '26 04:03

Brian Li


1 Answers

Printing a Python dict will in many cases not generate valid JSON. You want the json module:

import json

# ... snip ...

self.response.out.write(json.dumps(info))
# or
json.dump(info, self.response.out)
like image 183
nrabinowitz Avatar answered Mar 16 '26 16:03

nrabinowitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!