简体   繁体   English

当我从Google Map API传递json数据时,为什么会出错?

[英]Why I get error when I pass json data from google map API?

def map():
if request.method == 'POST':
    key = "googleMapAPIkey"
    orilist = request.form['from'].split()
    deslist = request.form['to'].split()
    mode = request.form.get('traffic_mode')
    ori = ""
    des = ""
    for word in orilist:
        ori = ori + word + "+"
    ori = ori[:-1]
    for word in deslist:
        des = des + word + "+"
    des=des[:-1]
    url = "https://maps.googleapis.com/maps/api/directions/json?origin="+ori+\
          "&destination="+des+"&key="+key
    print ori,des,mode,url
    r = requests.get(url)
    direction1 = r.json()

    markerArray = []
    print json.dumps(direction1)
    return render_template('googlemap.html', direction1=json.dumps(direction1), ori=request.form['from'], des=request.form['to'], mode=mode,method='POST')
else:
    print request.method
    return render_template('index.html',method='GET')

Here is the template script 这是模板脚本

var routes = {{ directions1|tojson }};
var start_point = {{ ori }};
var end_point = {{ des }};
var mode = {{ mode }};
function initMap() {
    var map = new google.maps.Map(document.getElementById('map'), {
        center: {lat: 42.880230, lng: -78.878738},
        zoom: 8
    });
    var renderer = new google.maps.DirectionsRenderer();

    var request = {
    travelMode: mode,
    origin: start_point,
    destination: end_point
    };
    renderDirections(map,routes,request)
}

I used the google api to get the response and I tried to pass this value into client-side. 我使用了google api来获取响应,并尝试将此值传递到客户端。 I 'm sure I get the JSON-formatted string, and when I tried to open the web, it comes that "TypeError: Undefined is not JSON serializable" 我确定我得到了JSON格式的字符串,并且当我尝试打开W​​eb时,出现了“ TypeError:Undefined is not JSON serializable”

You're trying to get the directions from a variable you haven't defined. 您正在尝试从尚未定义的变量中获取路线。

There is no directions1 coming from backend. 没有来自后端的directions1 Try direction1 . 尝试direction1 (Or rename it in render_template() so they match). (或在render_template()重命名它以使其匹配)。


Best practice: copy/paste variable names. 最佳做法: 复制/粘贴变量名。 Even when you make typos, your code will still work. 即使您输入错误,您的代码仍然可以使用。

Also, it's good to pay attention to the error text itself: "Undefined is... means exactly that: whatever you passed instead of a JSON serializable object ...is undefined . 另外,最好注意错误文本本身: “ Undefined is ...的意思就是:您传递的而不是JSON可序列化对象的任何信息都是undefined

The first you do when you see this error is: go to render_template() , double-click the variable name to select it and copy/paste in js. 看到此错误时,您首先要做的是:转至render_template() ,双击变量名称以选择它,然后将其复制/粘贴到js中。 Even when it "looks" identical. 即使“看起来”相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当我尝试从“/api/products”获取数据时,为什么会出错? - Why do I get an error when I try to get data from “/api/products”? 如何从Google Map API中的json文件读取json数据 - How can I read the json data from json file in google map api 当我尝试在Google脚本中使用数组映射函数时出现语法错误。 为什么? - I get a syntax error when I try to use array map function in Google Scripts. Why? 尝试从Google Map API获取照片时出现404错误 - 404 Error when trying to get the photos from Google Map API 为什么用jquery ajax执行GET请求时不能将json数组作为数据传递? - Why can't I pass a json array as data when doing GET request with jquery ajax? 当我尝试在JavaScript中解析JSON时为什么会出错 - Why I get error when I try to parse JSON in the JavaScript 我想通过ajax调用从地图获取json数据 - i want to get json data from map through ajax call 为什么我无法在json数据上调用.get /如何将json转换为地图 - Why am I unable to call .get on json data / how do I convert the json to a map 为什么我将 JSON 数据从 Javascript 发送到 Flask 时出现错误? - Why am i getting an error when I am sending JSON data from Javascript to Flask? 当我尝试从属性值获取HTML数据时,为什么会出现错误? - Why do I get an error when I try to get HTML data from an attribute value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM