将变量从python传递到javascript - javascript

我正在使用Google App Engine制作网页。它涉及html,python,javascript和Ajax。

工作流程如下:

当有人在html框中引入值时,它会调用javascript函数:

 INPUT type="text" id="busca" name="busca" onblur="SacarMapa()"

这将调用此函数:

  function SacarMapa()
  {
  var lugar=document.getElementById("busca").value;
    $.ajax("/mapa",
    { "type": "get",
        "data": {busca:lugar},
        // usualmente post o get
        "success": function(result) {
            initMap(RESULT-A, RESULT-B);
            },
        "error": function(result) {
            console.error("Se ha producido un error: ", result);},
        "async": true,})};

我需要的是RESULT-A和RESULT-B。这将来自使用python的服务器端:

class MapHandler(SessionModule.BaseSessionHandler):
   def get(self):
      #Obtencion de datos
      serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
      address=self.request.get('busca')
      self.response.out.write("Busca es "+address)
      url = serviceurl + urllib.urlencode({'address': address})
      uh = urllib.urlopen(url)
      data = uh.read()
      js = json.loads(str(data))
      resultado = js['status']
     if not resultado == "ZERO_RESULTS":
        location = js['results'][0]['formatted_address']
        latitud = js['results'][0]['geometry']['location']['lat']
        longitud = js['results'][0]['geometry']['location']['lng']

因此,我需要将python代码中的“经纬度”和“经度”发送到javascript,以便经纬度为RESULT-A和经度RESULT-B

这样做的方式是什么?

谢谢。

参考方案

解决了:

我终于以这种方式做到了:

(Python /服务器端)

class MapHandler(webapp2.RequestHandler):
def get(self):
    #Obtencion de datos
    serviceurl = 'http://maps.googleapis.com/maps/api/geocode/json?'
    address=self.request.get('busca')
    url = serviceurl + urllib.urlencode({'address': address})
    uh = urllib.urlopen(url)
    data = uh.read()
    js = json.loads(str(data))
    try:
            location = js['results'][0]['formatted_address']
            latitud = js['results'][0]['geometry']['location']['lat']
            longitud = js['results'][0]['geometry']['location']['lng']
            self.response.headers['Content-Type'] = 'application/json'
            self.response.out.write (json.dumps({"exito": 1, "lat": latitud, "long": longitud}))

    except Exception as e:
            self.response.out.write (json.dumps({"exito":0}))

Javascript:

function SacarMapa()
{
var lugar=document.getElementById("busca").value;
alert("Buscaremos "+lugar);
debugger;
$.ajax("/mapa",
    { "type": "get",
        "data": {busca:lugar},
        "dataType": "json",
        "success": function(datos) {
            if (datos['exito'] == "1")
            {
                initMap(datos['lat'], datos['long']);
            }
        },

        "error": function(datos) {
            debugger;
            console.error("Se ha producido un error: ", datos);},
        "async": true,})};

我对此感到非常疯狂,因为我忘记将webapp2.RequestHandler放在python类的标题中。

在JavaScript函数中转义引号 - javascript

我正在尝试将变量传递给javascript函数。根据用户的选择,它可以是文本或图像。这里已经讨论了类似的问题,但我无法解决。在php中,我这样编码:if ($choice == 1) { $img = '<img src = "../folder/'.$_SESSION["img"].'�…

如何在没有for循环的情况下在Javascript中使用Django模板标签 - javascript

我想在JavaScript中使用模板变量:我的问题是在javascript代码中使用for循环,for循环之间的所有事情都会重复..但我不想要....下面粘贴了我的代码..有人可以告诉我更好的方法吗这..因为这看起来很丑..这是我的代码: {% block extra_javascript %} <script src="/static/js…

使用JS和PHP更改弹出窗口背景图像 - javascript

我有一个JS函数:function zoom(now) { document.getElementById("popup").style.display = "block"; document.getElementById("photos").style.backgroundImage = …

打印二维阵列 - javascript

我正在尝试打印子元素。在this example之后。怎么做?。$myarray = array("DO"=>array('IDEAS','BRANDS','CREATIVE','CAMPAIGNS'), "JOCKEY"=>a…

获取JavaScript值到C#字符串 - javascript

                        是否可以在C#中执行类似的操作?该值为“ 10/05/2014”string jsValue = javascript("$('#EstimatedStartDate').val()"); 参考方案 您能否更详细地阐明您要做什么。看来您正在尝试从javascript(客户…