Pregunta

I need to post JSON object from webpage to GAE Python

website

$('#save').click(function() {
    x = JSON.stringify(result);
    $.post("/save_leads", x)
    .done(function() {})
    .fail(function() { alert("error"); })
});

fragment of stringified JSON object

[{"adrese":"Slokas 161, Rīga, LV-1067","nosaukums":"\"Akropolis\", apbedīšanas birojs","web":"www.akropoliss.lv","talrunis":"80005580","save":true},{"adrese":"Katlakalna 11c, Rīga, LV-1073","nosaukums":"DB Schenker Loģistikas Parks un Kravu Pasaule","web":"www.dbschenker.lv","talrunis":"67800087","save":true}]

python

def save_leads(self):
    results = self.request.get('x')
    logging.info(len(results)) #logs as 0 :S

So far I've only been posting contents of the form using html, so I have no idea if I'm posting or catching it wrong.

P.S.: I would prefer the answer that would allow me to json.loads() on Python side

¿Fue útil?

Solución

So to summ it all up. The right answer was:

web

$('#save').click(function() {
    y = JSON.stringify(result);
    console.log(result)
    $.post("/save_leads", {x:y})
    .done(function() {})
    .fail(function() { alert("error"); })
});

server side

def save_leads(self):
    result = self.request.get('x')
    result = json.loads(result)

If i could find a post that had answer like this, i would have to ask. So I hope this helps other jQuery -> post(JSON) -> Python newbs:)

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top