0

Well, I have one form, similar to dajax "django forms" example, all is working fine except the returning JSON is not being processed, I've modified the jquery.dajax.core.js like this:

...
process: function(data)
{
    if(data==Dajaxice.EXCEPTION){
        alert('Something went wrong, please reload the page.');
    }
    else{
        console.log(data);
        alert("ENTRO!");
        $.each(data, function(i,elem){
            alert("ENTRO!2");
            console.log(elem.cmd);
        switch(elem.cmd)
        {
            case 'alert':
                alert(elem.val)
            break;
            ...

One returned json from my ajax.py is:

[{"cmd": "remcc", "id": "#contact_form", "val": ["error"]}, {"cmd": "alert", "val": "Thank you, Alejandro"}, {"cmd": "js", "val": "alert(\"jummmmm\");"}]

When the json is processed back in the browser, I get the first "Entro!", but nothing else, nothing of the JSON is processed. If intentionally break things in ajax.py function, I get the correct behavior, alert Dajaxice.EXCEPTION.

Any suggestions?

--EDIT--

The Dajax.process function:

    validateContactForm = function() {
        datos = jQuery('#contact_form').serializeObject();
        console.log(datos);
        // jQuery
        // If you are using jQuery, you need this form->object serializer
        // https://github.com/cowboy/jquery-misc/blob/master/jquery.ba-serializeobject.js
        Dajaxice.ajax.validateContactForm(Dajax.process,{'formulario':datos});
    }

And the ajax.py function:

from dajax.core import Dajax

@dajaxice_register
def validateContactForm(request, formulario):
    dajax = Dajax()
    form = ContactoForm(formulario)

    if form.is_valid():
        dajax.remove_css_class('#contact_form','error')
        dajax.alert(_("Thank you, %(name)s") % {"name":form.cleaned_data.get('nombre')})
        form.save();
       dajax.script('alert("jummmmm");')
       print dajax, dir(dajax)
   else:
       dajax.remove_css_class('#contact_form','error')
       print form.as_ul()
       for error in form.errors:
           dajax.add_css_class('#id_%s' % error,'error')
           print error
           print dajax, dir(dajax)
   return dajax.json()

Ok, Firebug fails silent, chrome says:

Uncaught TypeError: Cannot call method 'each' of undefined
 Dajax.process
 oXMLHttpRequest.onreadystatechange
 b.dispatchEventda
 k
 b.open._object.onreadystatechange

and, Opera's dragonfly:

Uncaught TypeError: Cannot call method 'each' of undefined
Dajax.processjquery.dajax.core.js:11
oXMLHttpRequest.onreadystatechangedajaxice.core.js:100
b.dispatchEventdajaxice.core.js:141
kdajaxice.core.js:135
b.open._object.onreadystatechange

jQuery versions tested with the same behavior:

1.6.2 1.6.2.min 1.6.4

slothy
  • 363
  • 1
  • 3
  • 13

1 Answers1

0

I had the same problem, then ran across this function on this site: Convert form data to JavaScript object with jQuery

Community
  • 1
  • 1
wissam
  • 148
  • 1
  • 5