0

I want to send the contents of the variables via ajax json format, and recover data in python but still I did not managed to fare or sending or retrieving the data.

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"
I have this 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
John Conde
  • 217,595
  • 99
  • 455
  • 496
jioflow
  • 23
  • 4

1 Answers1

0

request is not what you think it is. It is of the 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.

You also have to tell the server that you are sending a json object. In 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.

You did this correctly!

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.

So you might want to do that. dataType is actually not the type of data you are sending, but the type of data you are expecting back. For more information read the jquery documentation of ajax.

Also you are currently trying to send an javascript object. What you want to send is a string in jsonformat. Use: data: JSON.stringify(yourJsonObjecthere)

For more information you can also read this stackoverflow question and another one that deal with similar questions.

Community
  • 1
  • 1
syntonym
  • 7,134
  • 2
  • 32
  • 45
  • thanks@syntonym I did what you said to me but all the time I find raise ValueError("No JSON object could be decoded") ValueError: No JSON object could be decoded I don't khnow if my code jquery is true or false. – jioflow Apr 18 '15 at 20:41
  • Try to set `contentType` to `"application/json"` in your AJAX call. You could also try to use `request.get_json(force=True)`. Or you could use `json.loads(request.data)`. EDIT: Maybe you also need to `processData` to false, see also my update. – syntonym Apr 18 '15 at 21:58