如何通过ajax json格式发送变量,并在python flas中接收这些数据

2024-06-25 23:14:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我想通过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); } }); }} }

我有个错误:
  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

Tags: inpyselfappflaskrequestlibpackages
1条回答
网友
1楼 · 发布于 2024-06-25 23:14:55

request不是你想象的那样。它属于^{}类型。尝试^{}^{}

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.

所以你可能想这么做。数据类型实际上不是您要发送的数据类型,而是您期望返回的数据类型。有关更多信息,请阅读ajax的jquery文档。在

另外,您当前正在尝试发送一个javascript对象。您要发送的是jsonformat格式的字符串。使用:data: JSON.stringify(yourJsonObjecthere)

有关更多信息,您还可以阅读处理类似问题的this stackoverflow question和{a6}。在

相关问题 更多 >