程序go 2016-03-13 12:29 采纳率: 100%
浏览 19

使用烧瓶和ajax

I'm serving a web app made with flask. I just added a feature to make a geocoding request to google using ajax. So, pushing a button calls this function in loc_gm.js:

$(function() {
$('#geo_google').click(function() {

    $.ajax({
        url: 'geo_gm',
        data: $('form').serialize(),
        type: 'POST',
        success: function(response) {
            response = JSON.parse(response)
            $('#Lat').val(response['lat']);
            $('#Long').val(response['lng']);
        },
        error: function(error) {
            console.log(error);
        }
    });
});

});

And this is the code in view.py:

@app.route('/geo_gm', methods=('GET', 'POST'))
def geo_gm():
    calle1 = request.form['calle1']
    calle2 = request.form['calle2']
    altura = request.form['altura']

if calle1 and calle2:
    address = '{}+y+{},+CABA,+AR'.format(calle1, calle2)
elif calle1 and altura:
    address = '{}+{},+CABA,+AR'.format(calle1, altura)

url = 'https://maps.googleapis.com/maps/api/geocode/json?address={}&key={}'.format(address, GOOGLE_KEY)
response = requests.get(url)
result = response.json()
return json.dumps(result['results'][0]['geometry']['location'])

This works in my local machine ( I get the coordinates I want from Google), but when I upload it to the server (Digital Ocean), I get this error in the javascript console:

POST http://192.xx.xx.xxx/geo_gm 404 (NOT FOUND)

Being that IP address the one where my app is hosted.

I know this must be a silly mistake I'm making, but I can't figure it out.

Thanks!

  • 写回答

1条回答 默认 最新

  • 叼花硬汉 2016-03-13 15:09
    关注

    Well, I finally found a workaround.

    I added in the html file this:

    <input type="hidden" id="geo-gm" name="variable" value="{{ url_for('geo_gm') }}">
    

    That way I can have the relative path to geo_gm function. And then in the js file:

    $.ajax({
            url: $('#geo-gm').val(),
    

    I did it this way because using {{ url_for('geo_gm') }} directly in the js file didn't work.

    Maybe it's not the best way to do it, so if someone has a better way I'll be happy to hear it.

    Thanks everybody.

    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度