Yii2 response 返回 JSONP callback

YII2 rest api 对接口开发有很好的支持,但是它有一个小毛病,在angular app开发中FORMAT_JSONP 不管怎么都不能返回callback ,后来在yii2的git中得知返回的数组名字一定要data,

angular中:
$http.jsonp(API_URL+
        '&callback=JSON_CALLBACK')
        .success(function(data) {
            deferred.resolve(data);
        })
        .error(function(data) {
            deferred.reject(data);
        });

api中:
public function actionIndex(){
        \Yii::$app->response->format = \yii\web\Response::FORMAT_JSONP;
...
$model_ALL = array(
                'data' => $model, 
                'count_total' => $countQuery->count(),
                'per_page' =>  $pages->defaultPageSize,
                'status' => 200,
                'callback' => $callBack,
                'pages' => $pageSize, 
         );
        return $model_ALL;

如果数组名字不是data 它就不认。
另外一种叫解决方案,修改源码: yii\web\JsonResponseFormatter
protected function formatJsonp($response)
 {
     $response->getHeaders()->set('Content-Type', 'application/javascript; charset=UTF-8');
     if (($callback = Yii::$app->request->get('callback')) !== null) {
         $response->content = sprintf('%s(%s);', $callback, Json::htmlEncode($response->data));
         } elseif ($response->data !== null) {
            $response->content = '';
            Yii::warning("The 'jsonp' response requires that jsonpCallback parameter to be set", __METHOD__);
        }
 }

Previous Post电商广告图常用设计技巧剖析
Next Post如何借鉴大师们优秀作品的配色(向大师借鉴的灵感配色)