将JSON发布到Python CGI(Post JSON to Python CGI)

编程入门 行业动态 更新时间:2024-10-27 04:26:42
将JSON发布到Python CGI(Post JSON to Python CGI)

我已经安装了Apache2和Python。

虽然我有问题。 我有两页。

一个是Python页面,另一个是使用JQuery的Html页面

有人可以告诉我如何让我的ajax文章正常工作。

<html> <head> </head> <body> <script> $(function() { alert('Im going to start processing'); $.ajax({ url: "saveList.py", type: "post", data: {'param':{"hello":"world"}}, dataType: "application/json", success : function(response) { alert(response); } }); }); </script> </body> </html>

和Python代码

import sys import json def index(req): result = {'success':'true','message':'The Command Completed Successfully'}; data = sys.stdin.read(); myjson = json.loads(data); return str(myjson);

I have got Apache2 Installed and Python working.

I am having a problem though. I have two pages.

One a Python Page and the other an Html Page with JQuery

Can someone please tell me how I can get my ajax post to work correctly.

<html> <head> </head> <body> <script> $(function() { alert('Im going to start processing'); $.ajax({ url: "saveList.py", type: "post", data: {'param':{"hello":"world"}}, dataType: "application/json", success : function(response) { alert(response); } }); }); </script> </body> </html>

And the Python Code

import sys import json def index(req): result = {'success':'true','message':'The Command Completed Successfully'}; data = sys.stdin.read(); myjson = json.loads(data); return str(myjson);

最满意答案

好的,我们来看看您更新的问题。

首先,您应该以字符串表示形式传递Ajax数据属性。 然后,由于您混合了dataType和contentType属性,请将dataType值更改为"json" :

$.ajax({ url: "saveList.py", type: "post", data: JSON.stringify({'param':{"hello":"world"}}), dataType: "json", success: function(response) { alert(response); } });

最后,修改你的代码来处理JSON请求,如下所示:

#!/usr/bin/python import sys, json result = {'success':'true','message':'The Command Completed Successfully'}; myjson = json.load(sys.stdin) # Do something with 'myjson' object print 'Content-Type: application/json\n\n' print json.dumps(result) # or "json.dump(result, sys.stdout)"

因此,在Ajax请求的success处理程序中,您将收到具有success和message属性的对象。

OK, let's move to your updated question.

First, you should pass Ajax data property in string representation. Then, since you mix dataType and contentType properties, change dataType value to "json":

$.ajax({ url: "saveList.py", type: "post", data: JSON.stringify({'param':{"hello":"world"}}), dataType: "json", success: function(response) { alert(response); } });

Finally, modify your code a bit to work with JSON request as follows:

#!/usr/bin/python import sys, json result = {'success':'true','message':'The Command Completed Successfully'}; myjson = json.load(sys.stdin) # Do something with 'myjson' object print 'Content-Type: application/json\n\n' print json.dumps(result) # or "json.dump(result, sys.stdout)"

As a result, in the success handler of Ajax request you will receive object with success and message properties.

更多推荐

本文发布于:2023-07-31 22:08:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1348611.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   JSON   Post   CGI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!