0

how can I check if my form was sent, its ajax/json

function save_formular() {
  var values = $$("form_elements").getValues();
  webix.ajax().header({
    "Content-type":"application/json"
  }).post("/create", JSON.stringify(values), function() {
    document.getElementById("content").innerHTML="<div>OK!</div>";
  });
}

I'm stuck in my python method create:

def create(self):

  # i want to check first if the form was sent
  data = cherrypy.request.json # error
  # AttributeError: 'Request' object has no attribute 'json'

  return sometext
create.exposed = True

is there another easy way to get these ajax values?

Kara
  • 6,115
  • 16
  • 50
  • 57
  • http://stackoverflow.com/questions/3743769/how-to-receive-json-in-a-post-request-in-cherrypy have you seen this? – Andrew Kloos Apr 20 '14 at 13:37
  • [add `@cherrypy.tools.json_in()` decorator](http://stackoverflow.com/a/18367567/4279) – jfs Apr 21 '14 at 09:55

1 Answers1

0

Okay its working now..

but i still need the return text from the Python method (e.g. search_ajax):

Python:

@cherrypy.expose
@cherrypy.tools.json_out()
@cherrypy.tools.json_in()
def search_ajax(self):
  # JSON-Values laden
  mydata = self.request.json

  filename = mydata["id"]

  if self.Datenbank.search(filename) == True:
    return '<b>OK</b>'
  else:
    return '<b>NOT FOUND</b>'

search_ajax.exposed = True

Javascript:

function search_k() {
  var values = $$("elements_k").getValues();
  webix.ajax().header({
    "Content-type":"application/json"
  }).post("/search_ajax", JSON.stringify(values), function() {
    document.getElementById("content").innerHTML="<div>RESULT</div>";
  });
}

Instead of the RESULT i want either OK or NOT FOUND