【问题标题】:Ajax: Unable to send Json object to bottle webserviceAjax:无法将 Json 对象发送到 Bottle Web 服务
【发布时间】:2016-04-28 10:47:28
【问题描述】:

我正在尝试了解 Ajax 调用的工作原理。

我将一个 Json 对象作为 URL 发送到瓶子 python 网络服务。

$.ajax({

        type: "POST", 
        data: {"jstring": JSON.stringify(output)},
        url: "http://localhost:8080/salesvolume" ,

        contentType: "application/json; charset=utf-8",
        dataType: "json",

        success: function(data){

                                $('#container').highcharts(data);
                                },
        error: function() {
                            alert("Something is not OK")    
                                },

        }); 

上面的 sn-p 是我的 Ajax 调用。 output 是我打算发送到服务器的 Json 对象。

@app.post('/salesvolume')
def salesvolume(db):
    jsonstring = request.forms.get('jstring')
    _jsonparams = json.loads(jsonstring)
    _studios = _jsonparams.Studios

ret = `Some Json`
return json.loads(ret)
app.run(server='paste', host='localhost', port=8080, debug=True, reloader=True)

这是我的网络服务代码 sn-p。

我收到了Status Code: HTTP/1.0 500 Internal Server Error

我一直在关注 Bottle 和 Jquery 文档,但我就是无法破解。 对此的任何帮助都会非常有用。

【问题讨论】:

  • 您的错误出现在哪里,堆栈跟踪是什么?
  • @IanAuld 好吧,当进行 Ajax 调用时,该错误是从 firefox 控制台中挑选出来的。基本上 Ajx 调用没有通过,我得到错误函数警报作为回报。我不确定堆栈跟踪是什么,我对这些东西很陌生。我可以检查一下吗?
  • @TauseefHussain 通话正在进行,您刚刚收到来自服务器的 500 错误。尝试将你的函数重写为def salesvolume(): return {},看看你的成功函数是否被命中。
  • @SwankSwashbucklers 如果通话正在进行,我应该会在控制台中看到添加到 URL 的 JSON 对象,对吧?我没看到。此外,当我尝试在 Web 服务中读取 Json 对象时,我得到一个 AttributeError
  • @TauseefHussain 服务器的输出是什么样的?

标签: python ajax json web-services bottle


【解决方案1】:

考虑以下几点:

1) 在 JS 中,将 url 更改为简单的:/salesvolume

2) 在 Python 中,从 salesvolume 函数定义中删除 arg - db。否则你可能会得到这个错误(500 错误):

TypeError: salesvolume() takes exactly 1 argument (0 given) <myServerIP> - - [30/Jul/2015 13:31:27] "POST /salesvolume HTTP/1.1" 500 1328

3) 检查缩进。是Python!我猜

ret = Some Json

return json.loads(ret) 需要缩进(它们应该在salesvolume 函数内)

我写了这个类似的东西,它似乎正在工作:

Python:

from bottle import *
import json, requests

@route('/')
def atHome():
    return template('index')

@route('/salesvolume', method="POST")
def salesvolume():
    #your processings here...
    ret = '{"key":"val"}'
    return json.loads(ret)

run(host='0.0.0.0', port=8093, debug=True, reloader=True)

index.tpl 和 JS:

<html>
<script type="text/javascript" src="https://code.jquery.com/jquery-1.11.3.min.js"></script>

<body>
<button onclick=ajaxF()>click</button>
</body>
<script>
function ajaxF(){
$.ajax({
    type: "POST", 
    data: {"jstring": JSON.stringify("blah")},
    url: "/salesvolume" ,
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(data){
        console.log('success');
        console.log(data)
        },
    error: function() {
        console.log("Something is not OK");
        },
    }); 
}
</script>

</html>

希望对你有帮助!

【讨论】:

    【解决方案2】:

    以下代码在使用瓶子的应用程序中为我工作(将一些数据发送到 python 并将一些数据作为 JSON 从 python 发送回 js):

    js:

    $.post('/getData', {myStringInput:dataToSendtoBottle}, function(data){
    var myJson = JSON.parse(data)   //myOutput is dispatched back to js as JSON
    });
    

    蟒蛇:

    @route('/getData', method='POST')
    def getData():
        myDataReceivedfromJs = request.forms.get('myStringIput')
        if myDataReceivedfromJs:
            myStringOutput = 'OK'
            myOutput = json.dumps(myStringOutput)
        return myOutput
    

    【讨论】:

      猜你喜欢
      • 2014-02-10
      • 2016-12-02
      • 2012-04-04
      • 2011-02-08
      • 1970-01-01
      • 2014-11-25
      • 1970-01-01
      • 1970-01-01
      • 2016-12-22
      相关资源
      最近更新 更多