2017-07-26 76 views
1

我想將一些(現在的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雲問題嗎?我所有的源文件都在一個桶中。 (12。)


我運行這些腳本與n1-standard-1 (1 vCPU, 3.75 GB memory) VM。

期望的行爲:我想對這些Python文件發出成功的Ajax請求,並返回結果 - 返回一些結果。我正在尋找與我所鏈接的兩個問題的回答者描述的行爲。

具體問題或錯誤: Ajax失敗。錯誤是400,文本在上面。

所需的最短代碼:我所包含的代碼可能有點長,但是我已經包含了,因爲我想包括其他可接受答案中的確切內容。 (問題說明:)我如何向在其他情況下在Google雲上運行的Python文件發出Ajax請求?


目前通過的Martijn的回答工作...

+0

Google App Engine *不是CGI環境*。爲什麼不選擇[App Engine上的Python快速入門](https://cloud.google.com/appengine/docs/standard/python/quickstart)。 –

+0

你還沒有包括*你如何運行*這些腳本。我假設你試圖使用App Engine,但也許你正在使用VM或容器引擎。您提到了一個存儲桶,但這是一個*存儲*解決方案,存儲桶中的文件不被視爲可執行代碼。 –

+0

@MartijnPieters我假設如果我在虛擬機上工作,快速入門將不會有幫助嗎? – Randoms

回答

0

它看起來像你的JavaScript代碼是好的,但我認爲你可以解決了Python代碼一點點 - 你使用標準輸入/出哪些我沒有看到有很好的結果。我建議你使用像Tornado這樣的庫,甚至是包含python的simpleHttpServer。

有在其主頁上這裏有一個例子龍捲風: http://www.tornadoweb.org/en/stable/

即使簡單的是simpleHttpServer,這裏是工作示例: Reading JSON from SimpleHTTPServer Post data

此外,我建議你儘量在本地運行這些例子在您的家用電腦上安裝Google Cloud之前。這很可能是代碼而不是GCP的問題。

祝你好運!

0

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開始,並從那裏移動。

+0

我從VM運行這個。對不起,我最初沒有包含這些信息。這裏鏈接的頁面是否仍然適用? – Randoms

+0

@Randoms:不,那不適用。你在虛擬機上安裝了Web服務器嗎?你是如何配置它的?這應該都是*在你的問題*中。 –