Google Cloud上的Python的Ajax
我想将一些(现在的JSON)数据发送到Python脚本并访问结果。经过尝试,并没有做到这一点我自己的,我在这里遇到了twoexamples。我一直无法工作。Google Cloud上的Python的Ajax
pythonAjaxTest.html
:
...
<script>
$(function(){
$('#ajaxButton').click(function(){
alert("Ajaxing...");
$.ajax({
url: "ajaxTest.py", //equivalently, replace with "saveList.py"
type: "POST",
dataType: "json",
data: JSON.stringify({
"param" : { "hello" : "world" }
}),
success: function(response) {
alert(response.responseText);
}, error: function(response) {
alert(response.responseText);
}
});
});
});
</script>
...
ajaxTest.py
:
#!/usr/bin/env python
import sys
import json
import cgi
fs = cgi.FieldStorage()
sys.stdout.write("Content-Type: application/json")
sys.stdout.write("\n")
sys.stdout.write("\n")
result = {}
result['success'] = True
result['message'] = "The command Completed Successfully"
result['keys'] = ",".join(fs.keys())
d = {}
for k in fs.keys():
d[k] = fs.getvalue(k)
result['data'] = d
sys.stdout.write(json.dumps(result,indent=1))
sys.stdout.write("\n")
sys.stdout.close()
saveList.py
:
#!/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)"
有了这两个,我得到的控制台400
错误和以下responseText
:
<?xml version=‘1.0’ encoding ‘UTF-8’?><Error><Code>InvalidArgument</Code><Message>Invalid argument.</Message><Details>POST object expects Content-Type multipart/form-data</Details></Error>
鉴于我基本上使用了与这两个问题的每个接受答案完全相同的代码,所以我不确定自己做错了什么。这仅仅是Google云问题吗?我所有的源文件都在一个桶中。 (1,2。)
我运行这些脚本与n1-standard-1 (1 vCPU, 3.75 GB memory)
VM。
期望的行为:我想对这些Python文件发出成功的Ajax请求,并返回结果 - 返回一些结果。我正在寻找与我所链接的两个问题的回答者描述的行为。
具体问题或错误: Ajax失败。错误是400
,文本在上面。
所需的最短代码:我所包含的代码可能有点长,但是我已经包含了,因为我想包括其他可接受答案中的确切内容。 (问题说明:)我如何向在其他情况下在Google云上运行的Python文件发出Ajax请求?
目前通过的Martijn的回答工作...
它看起来像你的JavaScript代码是好的,但我认为你可以解决了Python代码一点点 - 你使用标准输入/出哪些我没有看到有很好的结果。我建议你使用像Tornado这样的库,甚至是包含python的simpleHttpServer。
有在其主页上这里有一个例子龙卷风: http://www.tornadoweb.org/en/stable/
即使简单的是simpleHttpServer,这里是工作示例: Reading JSON from SimpleHTTPServer Post data
此外,我建议你尽量在本地运行这些例子在您的家用电脑上安装Google Cloud之前。这很可能是代码而不是GCP的问题。
祝你好运!
Google App Engine不会通过CGI连接您的Python代码。 CGI的例子不会起作用,不。
取而代之的是使用不同的更现代的标准Web Server Gateway Interface;参见[Python运行时环境文件] https://cloud.google.com/appengine/docs/standard/python/runtime):
A Python web app interacts with the App Engine web server using the WSGI protocol, so apps can use any WSGI-compatible web application framework. App Engine includes a simple web application framework, called webapp2, to make it easy to get started. For larger applications, mature third-party frameworks, such as Django, work well with App Engine.
而不是来自网络的回暖随机Python脚本,与Quickstart examples开始,并从那里移动。
我从VM运行这个。对不起,我最初没有包含这些信息。这里链接的页面是否仍然适用? – Randoms
@Randoms:不,那不适用。你在虚拟机上安装了Web服务器吗?你是如何配置它的?这应该都是*在你的问题*中。 –
Google App Engine *不是CGI环境*。为什么不选择[App Engine上的Python快速入门](https://cloud.google.com/appengine/docs/standard/python/quickstart)。 –
你还没有包括*你如何运行*这些脚本。我假设你试图使用App Engine,但也许你正在使用VM或容器引擎。您提到了一个存储桶,但这是一个*存储*解决方案,存储桶中的文件不被视为可执行代码。 –
@MartijnPieters我假设如果我在虚拟机上工作,快速入门将不会有帮助吗? – Randoms