Consuming Rest Web Service with Bottle (JQuery and JSON) -


i want consume rest web service returns json response:

web_service.py

from bottle import route, run  @route('/example') def example():     return {"result_1" : 3, "result_2" : 5}  run(host='localhost', port=8080) 

my javascript file this:

java_s.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)         }     }) }); 

and html file want cosume web service is:

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> 

however, when open index file results aren't shown. can give me suggestion fix problem? thanks!

you returning python dict object, not json string. can encode string manually or use, example, json library output json.

add import json beginning of file , change return json.dumps({"result_1" : 3, "result_2" : 5}) return actual json string created python dict object.


Comments

Popular posts from this blog

php - regexp cyrillic filename not matches -

c# - OpenXML hanging while writing elements -

sql - Select Query has unexpected multiple records (MS Access) -