简体   繁体   English

使用AJAX从Bottlepy服务器获取数据

[英]using AJAX to get data from Bottlepy server

I'm trying to retrieve json data from a bottlepy server onto a webpage. 我正在尝试将瓶状服务器中的json数据检索到网页上。 I was trying to implement a basic version first so tried with just strings. 我尝试首先实现基本版本,因此仅使用字符串尝试过。 But nothing seems to be happening. 但是似乎什么也没发生。 Here is the code - 这是代码-

HTML(including js) - HTML(包括js)-

<!DOCTYPE>
<html>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

<body>
<script>
function print()
{
    $(document).ready(function(){
        $.get('http://localhost:8080/check', function(result){
            alert('success');
            $('#main').html(result);
        });
    });
}

print();
</script></body>
</html>

The python code - python代码-

from bottle import Bottle, route, get,request, response, run, template

app = Bottle()

@app.hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'

# a simple json test main page
str = "Hello"
@route('/')                   #irrelevant to this question. Used this to check server...
def test():
    return template('file', str)

@app.get('/check')
def showAll():
    return str

run(host='localhost', port=8080)

What do I have to do to access the data on the server? 我需要做什么才能访问服务器上的数据? Note : The HTML is a separate file, and I want the code to work irrespective of the location of the HTML. 注意:HTML是一个单独的文件,并且无论HTML的位置如何,我都希望代码能够工作。

Also, if this is not possible, how can I do it with the help of templates? 另外,如果这不可能,我如何借助模板来做到这一点?

Your problem stems from some confusion with the bottle app. 您的问题源于与Bottle应用程序有些混淆。

Bottle creates a default app for you whenever you use @route (more on this) , and reuses this default app implicitly on subsequent calls. 每当您使用@route时,Bottle都会为您创建一个默认应用程序(有关更多信息) ,并在后续调用中隐式重用此默认应用程序。 This default app behavior is present in many functions (including hook and run ). 许多功能(包括hookrun )中都存在此默认应用行为。

The point is: 重点是:

app = Bottle() # creates an explicit app

@route('/')    # adds the route to the default app

@app.hook('after-request')  # adds the hook to the explicit app

run(...) # runs the default app, the hook is not used

To solve your problem you have two options: 要解决您的问题,您有两种选择:

  • remove any mention of the explicit app; 删除任何提及显式应用程序的内容;
  • always use the app explicitly 始终明确使用该应用

I found using the app explicitly made it easier to create sub-apps and much more clear in general what was going on. 我发现使用该应用程序显式地使得创建子应用程序更加容易,并且总体上更清楚了发生了什么。

New code: 新代码:

import bottle
from bottle import response, template, run

app = bottle.Bottle()

@app.hook('after_request')
def enable_cors():
    response.headers['Access-Control-Allow-Origin'] = '*'

# a simple json test main page
str = "Hello"
@app.route('/')                   #irrelevant to this question. Used this to check server...
def test():
    return template('file', str)

@app.get('/check')
def showAll():
    return str

run(app=app, host='localhost', port=8080)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM