发新话题
打印

symfony中使用ajax采用utf-8编码

symfony中使用ajax采用utf-8编码

在filters.yml文件中加入:
复制内容到剪贴板
代码:
xmlhttpUtf8DecodeFilter:
class: xmlhttpUtf8DecodeFilter
添加一个xmlhttpUtf8DecodeFilter.class.php
复制内容到剪贴板
代码:
<?php
/**
* This filter acts on AJAX requests and decodes the
* request parameter values using the php utf8_decode()
* function.
*
* @author jmunz <[email]jochen.munz@virn.de[/email]>
*/
class xmlhttpUtf8DecodeFilter extends sfFilter {
  
    /**
     * Run the filter
     */
    public function execute ($filterChain) {
        $request = $this->getContext()->getRequest();
        // Only act if this is an ajax request
        if ( $request->isXmlHttpRequest() && $this->isFirstCall() ){
            $params = $request->getParameterHolder()->getAll();
            $this->decodeParameters($params);
        }
        // execute next filter
        $filterChain->execute();
    }
  
    /**
     * Decode parameters
     */
    private function decodeParameters(&$params){
        foreach (array_keys($params) as $key) {
            if ( is_array($params[$key]) ) {
                $this->decodeParameters($params[$key]);
            } else {
                $params[$key] = utf8_decode($params[$key]);
            }
        }
    }
  
}
努力为phpres做贡献
时刻准备着,当机会来临时你就成功了
打好基础,增加社会经验
资深技术工程师是我的梦想
承接各种团体网站外包服务和各种it技术培训
准备申请AJAX版大,希望大家支持~~

TOP

发新话题