使用瓶子(JQuery和JSON)使用RESTWeb服务

使用瓶子(JQuery和JSON)使用RESTWeb服务,jquery,json,web-services,rest,bottle,Jquery,Json,Web Services,Rest,Bottle,我想使用这个返回JSON响应的rest web服务: web_service.py from bottle import route, run @route('/example') def example(): return {"result_1" : 3, "result_2" : 5} run(host='localhost', port=8080) 我的JavaScript文件如下所示: java_.js $(document).ready(function() {

我想使用这个返回JSON响应的rest web服务:

web_service.py

from bottle import route, run

@route('/example')
def example():
    return {"result_1" : 3, "result_2" : 5}

run(host='localhost', port=8080)
我的JavaScript文件如下所示:

java_.js

$(document).ready(function() {
    $.ajax({
        type: "GET"
        url: "http://localhost:8080/example",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(resp){
            $('.result_1').append(resp.result_1)
            $('.result_2').append(resp.result_2)
        }
    })
});
我想使用web服务的html文件是:

index.html

<head>
    <title>example</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script src="java_s.js"></script>
</head>

<body>
    <div>
        <p class="result_1">result 1: </p>
        <p class="result_2">result 2: </p>
    </div>
</body>

例子
结果1:

结果2:


但是,当我打开索引文件时,结果不会显示。你能给我一些建议来解决这个问题吗?谢谢

返回的是python dict对象,而不是json字符串。您可以手动对字符串进行编码,或者使用json库来实际输出json

import json
添加到文件开头,然后更改为
return json.dumps({“result_1”:3,“result_2”:5})
以返回从python dict对象创建的实际json字符串。

返回什么?它返回:{“result_1”:3,“result_2”:5},我想在html文件中显示该结果。我在web服务中添加了语句:response.content\u type=“application/json”,但它不起作用。