Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!aioe.org!feeder.news-service.com!news2.euro.net!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.047 X-Spam-Evidence: '*H*': 0.91; '*S*': 0.00; 'oct': 0.02; 'javascript,': 0.05; 'subject:Python': 0.06; '"""': 0.07; 'json': 0.07; 'python': 0.08; 'integer,': 0.09; 'skip:[ 20': 0.12; 'json,': 0.16; 'subject:function': 0.16; 'wrote:': 0.16; 'wed,': 0.17; 'subject:data': 0.21; 'header:In-Reply-To:1': 0.22; 'pm,': 0.24; 'string': 0.26; 'code.': 0.26; 'function': 0.27; 'import': 0.28; 'server': 0.29; 'pass': 0.29; 'sends': 0.29; 'message- id:@mail.gmail.com': 0.29; 'print': 0.29; 'example': 0.30; 'os,': 0.30; 'to:addr:python-list': 0.33; 'however,': 0.34; 'be.': 0.34; 'skip:# 10': 0.34; 'test': 0.34; 'data,': 0.35; 'received:209.85.161': 0.35; 'skip:" 20': 0.35; 'doing': 0.36; 'issue': 0.36; 'skip:" 10': 0.36; '8bit%:6': 0.37; 'but': 0.37; 'page': 0.37; 'could': 0.38; 'everyone': 0.38; 'received:google.com': 0.38; 'url:org': 0.38; 'received:209.85': 0.38; 'should': 0.38; 'subject:: ': 0.39; 'skip:d 40': 0.39; 'client': 0.39; 'difficult': 0.39; 'meaning': 0.39; 'needed.': 0.39; 'data': 0.39; 'to:addr:python.org': 0.39; 'more': 0.60; 'easily': 0.61; 'your': 0.61; 'virus: References: Date: Wed, 26 Oct 2011 18:58:07 -0400 Subject: Re: passing Python data to a javascript function From: Benjamin Kaplan To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Junkmail-Status: score=10/49, host=mpv2.tis.cwru.edu X-Junkmail-Signature-Raw: score=unknown, refid=str=0001.0A020202.4EA89080.004B,ss=1,re=0.000,fgs=0, ip=209.85.161.54, so=2011-07-25 19:15:43, dmn=2011-05-27 18:58:46, mode=single engine X-Junkmail-IWF: false X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.12 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 49 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1319669957 news.xs4all.nl 6934 [2001:888:2000:d::a6]:57782 X-Complaints-To: abuse@xs4all.nl Xref: x330-a1.tempe.blueboxinc.net comp.lang.python:15017 On Wed, Oct 26, 2011 at 6:18 PM, Bill Allen wrote: > > I am writing a Python CGI and am needing to pass a data value from Python= to a javascript function.=A0=A0 My understanding is that I should use JSON= as the middleman.=A0 However, I have not found a good example of doing thi= s.=A0=A0 The piece of data is a simple integer, but I could easily covert t= hat to a string first if necessary.=A0=A0 Here is what I am trying, but uns= uccessfully.=A0 I am sure that I have more than one issue in this code. > > > > #!/usr/bin/python > > import json, os, cgi, cgitb > cgitb.enable() > > pid =3D [{'p':str(os.getpid())}] > pid_data =3D json.dumps(pid) > > print "Content-type: text/html" > print > > print """ > "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> > > Test Page > > > > >

> > > """.format(pid_data) > You're making this much more difficult than it needs to be. JSON is used for sending data to JavaScript, meaning the JavaScript asks the server for a bunch of data, the server sends the client JSON, and everyone is happy. In your case, the information is available when the javascript is being generated so you can just pass in the number, no JSON needed.