Python Ajax:无法将Json对象发送到Web服务
我试图理解Ajax调用是如何工作的 我将Json对象作为URL发送到python Web服务Python Ajax:无法将Json对象发送到Web服务,python,ajax,json,web-services,bottle,Python,Ajax,Json,Web Services,Bottle,我试图理解Ajax调用是如何工作的 我将Json对象作为URL发送到python Web服务 $.ajax({ type: "POST", data: {"jstring": JSON.stringify(output)}, url: "http://localhost:8080/salesvolume" , contentType: "application/json; charset=utf-8", dat
$.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")
},
});
上面的代码片段是我的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)
这是我的Web服务代码片段
我得到一个状态代码:HTTP/1.0 500内部服务器错误
我一直在关注瓶子和Jquery文档,但我就是无法破解它。
这方面的任何帮助都将非常有用。请考虑以下事项: 1) 在JS中,将url更改为:
/salesvolume
2) 在Python中,从salesvolume
函数定义中删除arg-db
。或者,您可能会收到此错误(一个500
错误):
TypeError:salesvolume()正好接受1个参数(给定0)
--[30/Jul/2015 13:31:27]“POST/salesvolume HTTP/1.1”500 1328
3) 检查缩进。是蟒蛇!我想
ret=Some Json
和
return json.load(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)
<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>
index.tpl和JS:
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)
<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>
点击
函数ajaxF(){
$.ajax({
类型:“POST”,
数据:{“jstring”:JSON.stringify(“blah”)},
url:“/salesvolume”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:功能(数据){
console.log('success');
console.log(数据)
},
错误:函数(){
log(“有些事情不正常”);
},
});
}
希望有帮助 以下代码在使用瓶子的应用程序中为我工作(将一些数据发送到python,并将一些数据作为JSON从python发送回JSON): js: python:
@route('/getData', method='POST')
def getData():
myDataReceivedfromJs = request.forms.get('myStringIput')
if myDataReceivedfromJs:
myStringOutput = 'OK'
myOutput = json.dumps(myStringOutput)
return myOutput
您的错误来自哪里?堆栈跟踪是什么?@IanAuld-Well在调用Ajax时,错误是从firefox控制台中发现的。基本上Ajx调用没有通过,我得到了错误函数警报作为回报。我不确定堆栈跟踪是什么,我对这种东西是新手。这是我可以检查的吗?@TauseefHussain呼叫正在进行,您刚刚收到服务器的500个错误。尝试将函数重写为
def salesvolume():return{}
并查看成功函数是否被命中。@Swankswashbuckler如果调用正在进行,我应该在控制台中看到添加到URL的JSON对象,对吗?我不明白。另外,当我尝试读取web服务中的Json对象时,我会得到一个AttributeError
@TauseefHussain服务器的输出是什么样子的?