最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - removing `u` character in python output - Stack Overflow

programmeradmin10浏览0评论

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)

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)
Share Improve this question edited Mar 20, 2012 at 16:42 T.J. Crowder 1.1m200 gold badges2k silver badges1.9k bronze badges asked Mar 20, 2012 at 16:38 Brian LiBrian Li 5732 gold badges7 silver badges10 bronze badges 2
  • 4 Python's repr (which is being implicitly called here) isn't designed to produce JSON. – robert Commented Mar 20, 2012 at 16:45
  • 1 possible duplicate of Removing u in list – Wooble Commented Mar 20, 2012 at 17:24
Add a ment  | 

2 Answers 2

Reset to default 8

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)

The problem isn't in the JavaScript (per your original title), it's in the output of the JSON. You need to output properly-formatted JSON, which if it looks like {u'food': 90, u'cargo': 70, u'fuel': 50}, self.response.out.write(info) isn't doing. (jsonlint. is handy for validating JSON text.)

I'm not much of a python-head (actually, I'm not a python-head at all), but I think you want to replace

self.response.out.write(info)

with

json.dump(info, self.response)

..or similar (the above assumes that self.response is a "...a .write()-supporting file-like object..."), based on this reference.

发布评论

评论列表(0)

  1. 暂无评论