2010-05-09 93 views
1

我在寫一個簡單的App Engine應用程序。從JavaScript返回JSON到Python

我有一個簡單的頁面,允許用戶在谷歌地圖實例移動標記。每次用戶放棄標記時,我都想將long/lat返回到我的Python應用程序。

function initialize() { 

    ... // Init map 

    var marker = new GMarker(center, {draggable: true}); 
    GEvent.addListener(marker, "dragend", function() { 
    // I want to return the marker.x/y to my app when this function is called .. 
    }); 

} 

要我(固然有限)的知識,我應該是:

1)。返回JSON結構與我所需要的數據在聽者回調上述

2)。在我的webapp.RequestHandler處理程序類中,嘗試在方法中檢索JSON結構。

我非常希望通過這個JSON數據返回給應用程序,而不會導致重新加載頁面(這是當我用各種後/ form.submit方法到目前爲止發生了什麼)。

誰能給我提供一些僞代碼或如何,我會實現我後一個例子嗎?

謝謝。

回答

1

如果你不希望頁面更新,那麼你需要使用XMLHttpRequest。在這個例子中,我使用這個Google App Engine example的客戶端function Request(function_name, opt_argv)和服務器端RPCHandler。我沒有測試過這一點,但它看起來像:

客戶端JavaScript

function initialize() { 

    ... // Init map 

    var marker = new GMarker(center, {draggable: true}); 
    GEvent.addListener(marker, "dragend", function(position) { 
    Request('update_marker_position', [ unique_identifier, position.lat(), position.lng() ]); 
    }); 

} 

服務器端的Python

# Create database model for LatLng position 
class LatLng(db.Model): 
    lat = db.IntegerProperty() 
    lng = db.IntegerProperty() 

... 

class RPCMethods: 
    """ Defines the methods that can be RPCed. 
    NOTE: Do not allow remote callers access to private/protected "_*" methods. 
    """ 

    def update_marker_position(self, *args): 
     # args[0] - unique identifier, say GAE db key 
     # args[1] - lat 
     # args[2] - lng 
     # Note: need to do some checking that lat and lng are valid 

     # Retrieve key and update position 
     position = LatLng.get(db.Key(args[0]) 
     if position: 
      position.lat = args[1] 
      position.lng = args[2] 
     else: 
      position = LatLng(
       lat= args[1], 
       lng= args[2] 
      ) 
     position.put() 

     payload = { 
      'lat': args[1], 
      'lng': args[2], 
     } 
     return payload 

你需要你的時候創建數據庫條目提供頁面,並存儲數據庫密鑰客戶端。你也可以使用其他一些唯一的標識符。在這種情況下,我假定您將其存儲爲全局變量'unique_identifier'。

同樣,你需要添加一個回調函數來處理返回的有效載荷(包含成員'lat'和'lng')。從示例中,我相信您只需將您的回調函數添加爲Request的opt_argv數組中的第零個參數即可。我希望這有幫助。

7

防止頁面重載的方法是對網頁側AJAX來處理這個問題。

使用jQuery,你可能會做這樣的事情:

$("#testform").submit(function() { 
    // post the form values via AJAX... 
    var postdata = {lat: $("#lat").val(), long: $("#long").val()} ; 
    $.post('/submit', postdata, function(data) { 
     // and set the location with the result 
     $("#location").html(data['location']) ; 
     }); 
    return false ; 
    }); 

假設你有一個網頁是這樣的:

<p>Enter lat and long:</p> 
<form id="testform" action="#" method="post"> 
    <p> 
    <label for="lat">Lat:</label> 
    <input type="text" id="lat" /> <br /> 
    <label for="long">Long:</label> 
    <input type="text" id="long" /> <br /> 

    <input type="submit" value="Get Location" /> 
    </p> 
</form> 

<p>The location is:</p><p id="location">(enter lat and long above)</p> 

,然後讓Python代碼在一個JSON返回的位置字典。

最後,我建議有一個優雅的回退:如果用戶禁用JavaScript,做一個普通崗位如/getlocation並重新加載,然後讓JavaScript覆蓋這個提交到一個特殊的URL返回json,就像/getlocationajax