Ajax:无法将Json对象发送到bottle webservice

时间:2021-10-17 18:17:12

I am trying to understand how the Ajax call works.

我正在尝试理解Ajax调用是如何工作的。

I am sending a Json object to a bottle python webservice as an URL.

我将一个Json对象发送到一个瓶python webservice作为一个URL。

$.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")    
                                },

        }); 

The above snippet is my Ajax Call. output is the Json object that I intend to send to the server.

上面的代码片段是我的Ajax调用。输出是我打算发送到服务器的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)

And this is my Web Service code snippet.

这是我的Web服务代码片段。

I get a Status Code: HTTP/1.0 500 Internal Server Error

我得到一个状态码:HTTP/1.0 500内部服务器错误

I have been following the Bottle and Jquery documentations but Im just not able to crack this. Any help on this will be really greatful.

我一直在跟踪这个瓶子和Jquery文档,但是我无法破解它。这方面的任何帮助都将是非常好的。

2 个解决方案

#1


1  

Consider the following things:

考虑以下事情:

1) In JS, change the url to simply: /salesvolume.

1)在JS中,将url更改为:/salesvolume。

2) In Python, remove the arg - db from the salesvolume function definition. Or else you might get this err (a 500 error):

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

类型错误:salesvolume()只接受一个参数(给定) - - - - [30/Jul/2015 13:31:27] "POST /salesvolume HTTP/1.1" 5001328

3) Check indentation. Python it is! I guess

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

ret = Some Json and

一些Json和

return json.loads(ret) needs indentation (they should be inside the salesvolume function)

返回json.load (ret)需要缩进(它们应该在salesvolume函数内)

I wrote this similar stuff and it seems to be working:

我写了类似的东西,它似乎在起作用:

Python:

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 and JS:

索引。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>

Hope it helps!

希望它可以帮助!

#2


0  

The following code worked for me in an app using bottle (sends some data to python and dispatches some data back as JSON from python to js):

下面的代码在一个使用bottle的app中为我工作(将一些数据发送给python并将一些数据从python发送回给js):

js:

js:

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

python:

python:

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

#1


1  

Consider the following things:

考虑以下事情:

1) In JS, change the url to simply: /salesvolume.

1)在JS中,将url更改为:/salesvolume。

2) In Python, remove the arg - db from the salesvolume function definition. Or else you might get this err (a 500 error):

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

类型错误:salesvolume()只接受一个参数(给定) - - - - [30/Jul/2015 13:31:27] "POST /salesvolume HTTP/1.1" 5001328

3) Check indentation. Python it is! I guess

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

ret = Some Json and

一些Json和

return json.loads(ret) needs indentation (they should be inside the salesvolume function)

返回json.load (ret)需要缩进(它们应该在salesvolume函数内)

I wrote this similar stuff and it seems to be working:

我写了类似的东西,它似乎在起作用:

Python:

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 and JS:

索引。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>

Hope it helps!

希望它可以帮助!

#2


0  

The following code worked for me in an app using bottle (sends some data to python and dispatches some data back as JSON from python to js):

下面的代码在一个使用bottle的app中为我工作(将一些数据发送给python并将一些数据从python发送回给js):

js:

js:

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

python:

python:

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