javascript - Python causing server error when trying to save new JSON file -


i'm new python, let me know if there's glaring mistakes.

i've created simple webpage uses ajax, reads server's xml response, , displays on page. i'm using python script send xml response, it's building json file, , it's working perfectly.

but problem occurs when add code python script modifies json prior sending xml response.

here code causes problem:

with open("../../../var/www/data.json", "r+") jsonfile:     data = json.load(jsonfile)     data[0]["a"] = "2"     jsonfile.seek(0)  # rewind     jsonfile.write(json.dumps(data))     jsonfile.truncate() 

when code put python file, error in browser console when ajax sends post request: failed load resource: server responded status of 500 (internal server error)

but funny thing is, though prevents server responding properly, correctly update json file.

why writing json file cause server not respond post request?

here javascript function sends post request:

function sendajax(){     sendingajax = true;      console.log("sending ajax...");      var xmlhttpreq = false;     var self = this;     // mozilla/safari     if (window.xmlhttprequest) {         self.xmlhttpreq = new xmlhttprequest();     }     // ie     else if (window.activexobject) {         self.xmlhttpreq = new activexobject("microsoft.xmlhttp");     }     self.xmlhttpreq.open('post', "/cgi-bin/xmltest.py", true);     self.xmlhttpreq.setrequestheader('content-type', 'application/x-www-form-urlencoded');     self.xmlhttpreq.onreadystatechange = function() {         if (self.xmlhttpreq.readystate == 4 && self.xmlhttpreq.status == 200) {              if (self.xmlhttpreq.responsexml != null) {                 console.log("server responded 4!");                 document.getelementbyid("paragraph").innerhtml = self.xmlhttpreq.responsexml.getelementsbytagname('data')[0].childnodes[0].nodevalue;                 sendingajax = false;             }         }     }     self.xmlhttpreq.send();  } 

and here python script:

#!/usr/bin/env python  import cgi import cgitb import json    #this worked open json file, rewrite it, , open again reading needs commented out server respond open("../../../var/www/data.json", "r+") jsonfile:     data = json.load(jsonfile)     data[0]["a"] = "2"     jsonfile.seek(0)  # rewind     jsonfile.write(json.dumps(data))     jsonfile.truncate()  newjsonfile = open("../../../var/www/data.json", "r") newdata = json.load(newjsonfile) newjsonfile.close()  cgitb.enable()   print "content-type: text/xml"      print # blank line, end of headers print "<?xml version='1.0' encoding='utf-8' ?><inputs><data>"+newdata[0]["a"]+"</data></inputs>" 

any idea why bit of code causes server send 500 error code?

p.s. - i'm using raspberyy pi 2 server.

ok, have found bug in comments section. rp linux system think permission errors can resolved chmod. log in in shell user owns apache process , change permission or in simpler way: cd /www/ directory , run following command:

cd /var/www sudo chmod a+w data.json 

note give writing permission every user in system.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -