javascript - Python POST JSON - Is a web framework required? -
im bit new python , trying understand simple json post illustration (i using angularjs). have read other q's on , see folks utilize type of framework (ie, cherrypy).
i able send info (strings) via post , upon .success, display in simple alert. if wrap strings in html tags in python code itself. however, when take html tags out of python script , seek homecoming json object, 500 server error. gut telling me, must utilize web framework?
code failing json attempt.
js:
tablemodule.factory('service', ['$http', function($http) { homecoming { post : function($path, $data, callback) { homecoming $http.post($path, $data,{ headers: { 'content-type': 'application/x-www-form-urlencoded; charset=utf-8'} } ).success(callback); } ... service.post('test.cgi', $.param($data), function(data, status){...
and python simple:
import cgi import cgitb import sys import traceback import json cgitb.enable() def getdata1(): ...generate json data... json_string = json.dumps(dictresult) homecoming json_string if __name__ == "__main__": try: #htmltop() <-- code not shown, when wrap python in html, can homecoming simple strings getdata1() #htmlbottom() except: cgi.print_exception()
is there way simple illustration working without installing framework?
the simple reply "no". framework code written bunch of mutual things - e.g. setting http headers or handling cookies. instead of using framework, write own code these things. advantage of frameworks written else (saving time) , tend thought out in terms of code structure.
as farther illustration of how framework code written else, can view source code cherrypy here: https://bitbucket.org/cherrypy/cherrypy/src
regarding example, best place server's error logs. https://docs.python.org/2/library/cgi.html#testing-your-cgi-script
i'm not familiar cgi module, don't need print
output , gets sent client? getdata1 function looks returns json_string main body , nil - no printing.
what server using way? perhaps problem can auto-detect content-type html not json , need specify in output.
print "content-type:text/json\r\n\r\n"
javascript python json angularjs
No comments:
Post a Comment