views:

126

answers:

1

Am trying to validate my form using an AJAX call

$("#button").click(function() {
    $.ajax({                    
        type: "POST",
        url: "<?php echo $this->baseUrl() ?>/expensetypes/async",
        data: 'fs=' + JSON.stringify($('#myform').serialize(true)),                 
        contentType: "application/json; charset=utf-8",
        dataType: "json"
        });
    });

On my controller my code is as follows

//Map the form from the client-side call
        $myFormData = Zend_Json::decode($this->getRequest()->getParam("fs") 
                            ,Zend_Json::TYPE_ARRAY);

        $form = new Form_Expensetypes();
        $form->isValid($myFormData);

However from firebug my output is as follows

fs="id=&expense_types_code=AAA&expense_types_desc=CCCC&expense_types_linkemail=XXXX&expense_types_budgetamount=22222&expense_types_budgetperiod=22222"

What I expect is something similar to

fs{"expense_types_code":"AAA","expense_types_desc":"CCCC","expense_types_linkemail":"XXXX","expense_types_budgetamount":"22222"}

How do I achieve this type of serialisation?

A: 

The jQuery serialize function always returns an URL encoded string. Use serializeArray with some array mangling afterwards instead.

Steffen Müller
I am now using serializedArray and though the output is now different and close to what I expect its not similar to the expected output so that Zend_Json::decode could do work well.Maybe my decode on the server end needs adjustments...
davykiash