Browse Source

Fixed jsonp response handling.

tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
22839ae922
  1. 8
      framework/yii/web/Response.php

8
framework/yii/web/Response.php

@ -706,8 +706,7 @@ class Response extends \yii\base\Response
/** /**
* Prepares for sending the response. * Prepares for sending the response.
* The default implementation will convert [[data]] into [[content]] and set headers accordingly. * The default implementation will convert [[data]] into [[content]] and set headers accordingly.
* @throws InvalidParamException if `$format` is not supported * @throws InvalidConfigException if the formatter for the specified format is invalid or [[format]] is not supported
* @throws InvalidConfigException if the formatter for the specified format is invalid
*/ */
protected function prepare() protected function prepare()
{ {
@ -744,10 +743,11 @@ class Response extends \yii\base\Response
$this->getHeaders()->set('Content-Type', 'text/javascript; charset=' . $this->charset); $this->getHeaders()->set('Content-Type', 'text/javascript; charset=' . $this->charset);
if (is_array($this->data) && isset($this->data['data'], $this->data['callback'])) { if (is_array($this->data) && isset($this->data['data'], $this->data['callback'])) {
$this->content = sprintf('%s(%s);', $this->data['callback'], Json::encode($this->data['data'])); $this->content = sprintf('%s(%s);', $this->data['callback'], Json::encode($this->data['data']));
break;
} else { } else {
throw new InvalidParamException("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements."); $this->content = '';
Yii::warning("The 'jsonp' response requires that the data be an array consisting of both 'data' and 'callback' elements.", __METHOD__);
} }
break;
case self::FORMAT_XML: case self::FORMAT_XML:
$this->content = Yii::createObject(XmlResponseFormatter::className())->format($this); $this->content = Yii::createObject(XmlResponseFormatter::className())->format($this);
break; break;

Loading…
Cancel
Save