javascript - Python server does not accept JSON sent by jquery ajax -
i use json manage data between client/server. however, works except json... think come python server, not specialist in server programming don't know how change in python server. python server simple because don't know how program inside. if don't use json works not efficient sort data. there easy way modify python server accept json (if comes python server)?
here html:
<form method="post" id="formu" > <textarea class="field span10" id="sequence" name="sequence" cols="4" rows="5"></textarea> <input type="submit" value="submit" class="btn btn-primary"> </form>
my javascript:
$(document).ready(function() { // formular $('#formu').on('submit', function(e) { e.preventdefault(); // prevent default behavior var sequence = $('#sequence').val(); $.ajax({ url : 'test.py', type : 'post', data : json.stringify({'sequence' : sequence}), datatype: 'json', success : function(data){ alert(data); } // end of success function }); // end of ajax }); });
my python code ajax (test.py):
import json result = {'myresult':'lalalalalal'}; myjson = json.load(sys.stdin) result['fromclient'] = myjson['sequence'] print 'content-type: application/json\n\n' print json.dumps(result)
and python server:
#!/usr/bin/python import basehttpserver import cgihttpserver import cgitb; cgitb.enable() import mimetypes mimetypes.add_type("image/svg+xml", ".svg", true) mimetypes.add_type("image/svg+xml", ".svgz", true) mimetypes.add_type("application/javascript", ".js", true) mimetypes.add_type("text/javascript", ".js", true) mimetypes.add_type("text/plain", ".txt", true) mimetypes.add_type("text/html", ".html", true) mimetypes.add_type("application/perl", ".pl", true) mimetypes.add_type("application/json", ".json", true) server = basehttpserver.httpserver handler = cgihttpserver.cgihttprequesthandler server_address = ("127.0.0.1", 8080) #handler.cgi_directories = ['/foldomeweb'] handler.cgi_directories = ['/webserver'] httpd = server(server_address, handler) try: print "running http server" httpd.serve_forever() except keyboardinterrupt: print "server stoped"
don't use
data : json.stringify({'sequence' : sequence})
and pass object jquery ajax call. it'll handle formatting itself. remember form values comprised of name, value pairs - let jquery you.
Comments
Post a Comment