如何通过 ajax json 格式发送变量以及如何在 python 烧瓶中接收此数据
how to send variables via ajax json format and how receive this data in python flask
我想通过ajaxjson格式发送变量的内容,并恢复python中的数据,但我仍然没有成功或发送或检索数据.
function deconnectmac(){
var essi = prompt("Please enter essi!!");
var tab = prompt("Please enter the tab!!");
var jsonObj = [];
var obj = {};
obj["tab"]=tab;
obj["essi"]=essi;
jsonObj.push(obj);
console.log(jsonObj);
$jsonObj=JSON.stringify(jsonObj);
if (tab != null) {
if(essi!= null){
$.ajax({
type: 'POST',
url: '/deconnect',
data:{ data: jsonObj },
dataType: 'text',
success: function(text) {
if (text === "success") {
alert("success");
} else {
alert(text);
}
},
error: function(error){
console.log(error);
}
});
}}
}
@APP.route('/deconnect',methods=['POST'])
def deconnect():
donne_request=json.loads(request)
print donne_request
return "success"
我有这个错误:
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/rik/serveur_externe/app/app.py", line 166, in deconnect_host
donne_request=json.loads(request)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
request
不是你想的那样。它属于flask.Request
type. Try request.data
or request.get_json()
:
Parses the incoming JSON request data and returns it. If parsing fails
the on_json_loading_failed() method on the request object will be
invoked. By default this function will only load the json data if the
mimetype is application/json but this can be overriden by the force
parameter.
您还必须告诉服务器您正在发送一个 json 对象。在 jquery Sending Data to the Server:
By default, Ajax requests are sent using the GET HTTP method. If the
POST method is required, the method can be specified by setting a
value for the type option.
你做对了!
If [an object of the form { key1: "value1, ...}
] is used, the data is converted into a query string
using jQuery.param() before it is sent. This processing can be
circumvented by setting processData to false. The processing might be
undesirable if you wish to send an XML object to the server; in this
case, change the contentType option from
application/x-www-form-urlencoded to a more appropriate MIME type.
所以你可能想这样做。 dataType 实际上不是你发送的数据类型,而是你期望返回的数据类型。有关详细信息,请阅读 ajax 的 jquery 文档。
此外,您目前正在尝试发送一个 javascript 对象。您要发送的是 json 格式的字符串。使用:data: JSON.stringify(yourJsonObjecthere)
有关更多信息,您还可以阅读处理类似问题的 this Whosebug question and another one。
我想通过ajaxjson格式发送变量的内容,并恢复python中的数据,但我仍然没有成功或发送或检索数据.
function deconnectmac(){
var essi = prompt("Please enter essi!!");
var tab = prompt("Please enter the tab!!");
var jsonObj = [];
var obj = {};
obj["tab"]=tab;
obj["essi"]=essi;
jsonObj.push(obj);
console.log(jsonObj);
$jsonObj=JSON.stringify(jsonObj);
if (tab != null) {
if(essi!= null){
$.ajax({
type: 'POST',
url: '/deconnect',
data:{ data: jsonObj },
dataType: 'text',
success: function(text) {
if (text === "success") {
alert("success");
} else {
alert(text);
}
},
error: function(error){
console.log(error);
}
});
}}
}
@APP.route('/deconnect',methods=['POST'])
def deconnect():
donne_request=json.loads(request)
print donne_request
return "success"
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/rik/serveur_externe/app/app.py", line 166, in deconnect_host
donne_request=json.loads(request)
File "/usr/lib/python2.7/json/__init__.py", line 326, in loads
return _default_decoder.decode(s)
File "/usr/lib/python2.7/json/decoder.py", line 366, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
TypeError: expected string or buffer
request
不是你想的那样。它属于flask.Request
type. Try request.data
or request.get_json()
:
Parses the incoming JSON request data and returns it. If parsing fails the on_json_loading_failed() method on the request object will be invoked. By default this function will only load the json data if the mimetype is application/json but this can be overriden by the force parameter.
您还必须告诉服务器您正在发送一个 json 对象。在 jquery Sending Data to the Server:
By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option.
你做对了!
If [an object of the form
{ key1: "value1, ...}
] is used, the data is converted into a query string using jQuery.param() before it is sent. This processing can be circumvented by setting processData to false. The processing might be undesirable if you wish to send an XML object to the server; in this case, change the contentType option from application/x-www-form-urlencoded to a more appropriate MIME type.
所以你可能想这样做。 dataType 实际上不是你发送的数据类型,而是你期望返回的数据类型。有关详细信息,请阅读 ajax 的 jquery 文档。
此外,您目前正在尝试发送一个 javascript 对象。您要发送的是 json 格式的字符串。使用:data: JSON.stringify(yourJsonObjecthere)
有关更多信息,您还可以阅读处理类似问题的 this Whosebug question and another one。