2009年11月1日星期日

Re: [fw-db] How can I to print Json formated response?

You could use contextSwitching for that particular action.  By default the ajax response will be text/html with ZF.


You can set the available format to be json and in your url just add "/format/json"

Zend will automatically change the response type to json, which is what you are looking to do.

If you don't want to go through all of that then you can for the http response to JSON.

On Fri, Oct 30, 2009 at 7:41 PM, Gray Fox <gmefox@gmail.com> wrote:

That may not necessarily be the problem, but did you try specifying the
response type (adding "json" as the 4th parameter in the $.post() call)?


BEREGU wrote:
>
> Hello,
>
> I wanted to print json formatted response, which is passed from
> controller passing to a view script.
> The response, which is catched with firebug is:
>
> |{"ai80":"1050","ai92":"1100","diesel":"1300"}|
>
>
> But I can't see any of those values on my browser. Please advice me what
> I made wrong.
>
>
>
> In the controller:
> =============
> $fuels = new BEREGU_FuelsDbTable();
> $result = $fuels->fetchAll();
>
> foreach ($result as $fuel){
>             switch ($fuel->id) {
>                 case '2':
>                     $ai80 = $fuel->price;
>                 break;
>
>                 case '3':
>                     $ai92 = $fuel->price;
>                 break;
>
>                 case '6':
>                     $diesel = $fuel->price;
>                 break;
>             }
>         }
>
> $item = array();
> $item['ai80'] = $ai80;
> $item['ai92'] = $ai92;
> $item['diesel'] = $diesel;
>
> echo Zend_Json_Encoder::encode($item);
> ///////////////////////////////
>
> In the view script
> =============
> function getPriceJson(volume)
>     {
>         $.post(
>         "/async/getprice",
>         {
>             "volume" :    volume
>         },
>
>         function(response)
>         {
>             $("#ai92").append(" # "+response.diesel+" ");
>             $("#ai80").append(response.ai80);
>             $("#diesel").append(response.diesel);
>         });
>     }
> //////////////////////////////////////
>
>
>
>
> Sincerely,
> Enkhbilguun Erdenetsogt
>
> begin:vcard
> fn:Enkhbilguun Erdenetsogt
> n:Erdenetsogt;Enkhbilguun
> email;internet:enkhbilguun@beregu.com
> tel;cell:99113638
> x-mozilla-html:TRUE
> version:2.1
> end:vcard
>
>
>

--
View this message in context: http://old.nabble.com/How-can-I-to-print-Json-formated-response--tp26111958p26139117.html
Sent from the Zend DB mailing list archive at Nabble.com.




--
Cory Wiles
kwylez@gmail.com
www.corywiles.com | www.feedfootball.com

没有评论: