我是yiiframework的新手。
我想在Yii2 REST api中一起使用JSONP和JSON格式,这意味着如果一个api调用正常,响应得到json,并且如果一个带有jsonp ajax的api调用,得到JSONP结果,我在论坛或stackoverflow中找不到任何东西来进行配置。我该怎么做?我Response::FORMAT_JSON
在控制器中使用行为,但我想在控制器中使用Response::FORMAT_JSON
和Response::FORMAT_JSONP
在一起。如何一起配置?
使用类的format
属性yii\web\Response
:
public class YourController extends Controller {
/**
* json/jsonp witching example
*/
function actionJson(
$callback = null
) {
// retrieve data to be returned
$data = array(
// your data here
);
// set "fomat" property
Yii::$app->getResponse()->format =
(is_null($callback)) ?
self::FORMAT_JSON :
self::FORMAT_JSONP;
// return data
return (is_null($callback)) ?
$data :
array(
'data' => $data,
'callback' => $callback
);
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句