Sending JSON through requests module and catching it using bottle.py and cherrypy

北战南征 提交于 2019-11-30 16:56:26

For a application/json POST, simply access request.json:

@route ('/tagTweets', method='POST')
def tagTweets():
     response.content_type = 'application/json'
     sender = request.json['sender']
     receiver = request.json['receiver']
     message = request.json['message']

Give this a try...

//cherrypy

import json

@route ('/tagTweets', method='POST')
def tagTweets(UpdatedData=None):
    Data = json.loads(UpdatedData)

//javascript

function SendJson()
{
    var JSONObject = { changes: [] };
    JSONObject.changes.push({"sender": "Alice", "receiver": "Bob" }
            );

    // code for IE7+, Firefox, Chrome, Opera, Safari
    if(window.XMLHttpRequest)
        xmlhttp=new XMLHttpRequest();
    else// code for IE6, IE5
        xmlhttp=new ActiveXObject('Microsoft.XMLHTTP');

    xmlhttp.onreadystatechange=function()
    {
        if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            alert(xmlhttp.responseText);
        }
    }

    xmlhttp.open("POST","/tagTweets", true);
    xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    xmlhttp.send(JSON.stringify(JSONObject));
}

Hope this helps.

Andrew

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!