谷歌地图API Javascript错误
所以,我正在构建一个web应用程序,它使用google maps的API来显示一个带有一些标记的地图,每次你点击一个标记,它应该显示来自该位置的5篇文章,这些文章由google news提供,我对javascript和web开发还很陌生,我一直在努力自己解决这个问题,但我不知道如何解决。我将保留我的JS文件和使用flask处理服务器请求的python文件,但我确信服务器端没有任何问题 js: Python:谷歌地图API Javascript错误,javascript,python,google-maps,google-maps-api-3,Javascript,Python,Google Maps,Google Maps Api 3,所以,我正在构建一个web应用程序,它使用google maps的API来显示一个带有一些标记的地图,每次你点击一个标记,它应该显示来自该位置的5篇文章,这些文章由google news提供,我对javascript和web开发还很陌生,我一直在努力自己解决这个问题,但我不知道如何解决。我将保留我的JS文件和使用flask处理服务器请求的python文件,但我确信服务器端没有任何问题 js: Python: import os import re from flask import Flask,
import os
import re
from flask import Flask, jsonify, render_template, request, url_for
from flask_jsglue import JSGlue
from cs50 import SQL
from helpers import lookup
# configure application
app = Flask(__name__)
JSGlue(app)
# ensure responses aren't cached
if app.config["DEBUG"]:
@app.after_request
def after_request(response):
response.headers["Cache-Control"] = "no-cache, no-store, must-revalidate"
response.headers["Expires"] = 0
response.headers["Pragma"] = "no-cache"
return response
# configure CS50 Library to use SQLite database
db = SQL("sqlite:///mashup.db")
@app.route("/")
def index():
"""Render map."""
if not os.environ.get("API_KEY"):
raise RuntimeError("API_KEY not set")
return render_template("index.html", key=os.environ.get("API_KEY"))
@app.route("/articles")
def articles():
"""Look up articles for geo."""
# get geo
geo = request.args.get("geo")
# if there is no geo, return error
if geo == '' or geo == None:
raise RuntimeError("missing geo")
# get articles for this geo
articles = lookup(geo)
if len(articles) > 5:
articles = articles[:5]
# return them as a json object
return jsonify(articles)
@app.route("/search")
def search():
"""Search for places that match query."""
place = request.args.get("q") + "%"
# get everything that mathes this query
places = db.execute("SELECT * FROM places WHERE postal_code LIKE :place OR place_name LIKE :place OR admin_name1 LIKE :place LIMIT 10", place=place)
# return them as objects
return jsonify(places)
@app.route("/update")
def update():
"""Find up to 10 places within view."""
# ensure parameters are present
if not request.args.get("sw"):
raise RuntimeError("missing sw")
if not request.args.get("ne"):
raise RuntimeError("missing ne")
# ensure parameters are in lat,lng format
if not re.search("^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$", request.args.get("sw")):
raise RuntimeError("invalid sw")
if not re.search("^-?\d+(?:\.\d+)?,-?\d+(?:\.\d+)?$", request.args.get("ne")):
raise RuntimeError("invalid ne")
# explode southwest corner into two variables
(sw_lat, sw_lng) = [float(s) for s in request.args.get("sw").split(",")]
# explode northeast corner into two variables
(ne_lat, ne_lng) = [float(s) for s in request.args.get("ne").split(",")]
# find 10 cities within view, pseudorandomly chosen if more within view
if (sw_lng <= ne_lng):
# doesn't cross the antimeridian
rows = db.execute("""SELECT * FROM places
WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude AND longitude <= :ne_lng)
GROUP BY country_code, place_name, admin_code1
ORDER BY RANDOM()
LIMIT 10""",
sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)
else:
# crosses the antimeridian
rows = db.execute("""SELECT * FROM places
WHERE :sw_lat <= latitude AND latitude <= :ne_lat AND (:sw_lng <= longitude OR longitude <= :ne_lng)
GROUP BY country_code, place_name, admin_code1
ORDER BY RANDOM()
LIMIT 10""",
sw_lat=sw_lat, ne_lat=ne_lat, sw_lng=sw_lng, ne_lng=ne_lng)
# output places as JSON
return jsonify(rows)
这是我运行页面时控制台上出现的错误,如果需要,我可以留下一个指向运行应用程序的服务器的链接。
以下代码在第93行返回未定义的值:
// get articles for the place
json = $.getJSON(Flask.url_for("articles"), parameters);
当您尝试访问json.responseJSON时,错误发生在第100行。您使用的条件必须更像以下条件之一:
if ( typeof json != "undefined" )
或
错误消息和代码的图片不是很有用。请在您的问题中提供错误的完整文本,可能在标题中也会有所帮助,以及生成该错误的原因。对不起,这是显示错误的唯一原因,而且,如果我尝试从代码中删除任何内容,我会以某种方式破坏应用程序。这是应用程序的链接,它目前正在运行,也许你可以在控制台中找到一些东西,再说一遍,我对编程很陌生,刚做了一个多月,网络编程才一个多星期,很抱歉我无法提供更多信息。@geocodezip请问我一些更具体的问题,我很乐意提供给你们。我只会尝试对返回的值做一些事情,如果有值,有if条件来检查它。此外,这部分代码应该是为某个特定的地方获取文章,我收到了这些文章。在第100行,检查json变量是否未定义:if typeof json!=undefined只是尝试了一下,没有改变任何东西,我之前有一个json.responseJSON错误,因为它是未定义的,当我添加这个条件时,它解决了这个问题,这是另一回事如果你从错误图像判断,我的javascript文件名为scripts.js,出现的都不是我的,它们都来自google maps的API。
if ( typeof json != "undefined" )
if ( json.responseJSON.length > 0 )