ajax, storage, json

94 views
Skip to first unread message

Leandro Severino

unread,
Nov 22, 2012, 10:15:52 AM11/22/12
to we...@googlegroups.com
Hi,

   Sorry by my dummies questions but...
   
   I created a link in a test page and in the onclick of this link I call by ajax (jquery) a query in database.

function get_content(reference_entity){
           ajax_url = "/content/" + reference_entity
           jQuery.ajax({ 
               type: "POST", 
               url: ajax_url, 
               success: function(data) {
                   if (data){ 
                    alert(data);
                       obj_return=document.getElementById('conteudo_panel');
                       obj_return.innerHTML=data;
                   }
               }, 
               error: function(e, a) { 
                   obj_return=document.getElementById('conteudo_panel');
                   obj_return.innerHTML="Failure! " + e + " " + a;
               } 
           }); 
       }

   This query return a Storage Object:
<Storage {'descricao_completa': u'F\xeanix Artes Gr\xe1ficas - Convites, impressos e impress\xe3o digital em Canoas/RS', 'conteudo': u'<h1>Convites</h1>', 'palavras_chaves': u'portif\xf3lio, produtos', 'titulo': u'Convites'}>

   Well, I need transform this storage object in a JSON object and put the content of 'conteudo' key in the innerHTML in a div, how to do this ?

   Thanks, 
   Leandro.

Scott Gelin

unread,
Nov 22, 2012, 10:21:19 AM11/22/12
to we...@googlegroups.com
Leandro,
In your POST function, in your web.py app - you want to import json, and where returnval is your current return value, you want to return json.dumps(returnval).  

In your success callback function, data.conteudo or data['conteudo'] should work for accessing the value stored under the conteudo key.
Hope that helps.

-Scott

   Leandro.

--
You received this message because you are subscribed to the Google Groups "web.py" group.
To view this discussion on the web visit https://groups.google.com/d/msg/webpy/-/aIx7I16Y4K8J.
To post to this group, send email to we...@googlegroups.com.
To unsubscribe from this group, send email to webpy+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/webpy?hl=en.

Leandro - ProfessionalIT - Soluções em Tecnologia da Informação.

unread,
Nov 22, 2012, 10:32:46 AM11/22/12
to we...@googlegroups.com
Hi Scott,
    aftet put the json.dump(), 

              function get_content(reference_entity){
            ajax_url = "/content/" + reference_entity
            jQuery.ajax({ 
                type: "POST", 
                url: ajax_url, 
                success: function(data) {
                    if (data){ 
                     alert(data); 
                                // return: {"descricao_completa": "F\u00eanix Artes Gr\u00e1ficas - Convites, impressos e impress\u00e3o digital em Canoas/RS", "conteudo": "<h1>Convites</h1>", "palavras_chaves": "portif\u00f3lio, produtos", "titulo": "Convites"}

                                alert(data.conteudo); // return undefined

                                 alert(data['conteudo']); // return undefined

                        obj_return=document.getElementById('conteudo_panel');
                        obj_return.innerHTML=data;
                    }
                }, 
                error: function(e, a) { 
                    obj_return=document.getElementById('conteudo_panel');
                    obj_return.innerHTML="Failure! " + e + " " + a;
                } 
            }); 
        }

        where is my wrong ?     

Scott Gelin

unread,
Nov 22, 2012, 10:42:27 AM11/22/12
to we...@googlegroups.com
I'm uncertain - in my javascript console, this works - 
x= {"descricao_completa": "F\u00eanix Artes Gr\u00e1ficas - Convites, impressos e impress\u00e3o digital em Canoas/RS", "conteudo": "<h1>Convites</h1>", "palavras_chaves": "portif\u00f3lio, produtos", "titulo": "Convites"}
Object
  1. conteudo"<h1>Convites</h1>"
  2. descricao_completa"Fênix Artes Gráficas - Convites, impressos e impressão digital em Canoas/RS"
  3. palavras_chaves"portifólio, produtos"
  4. titulo"Convites"
  5. __proto__Object

x.conteudo
"<h1>Convites</h1>"
x['conteudo']
"<h1>Convites</h1>"

Leandro - ProfessionalIT - Soluções em Tecnologia da Informação.

unread,
Nov 22, 2012, 10:49:11 AM11/22/12
to we...@googlegroups.com
Scott,

   Thank you so much !

   The correct syntax:

           jQuery.ajax({ 
               type: "POST", 
               url: ajax_url, 
               dataType: "json",
               success: function(data) {
 
Leandro.
Reply all
Reply to author
Forward
0 new messages