Commit ae39324e by Qiang Xue

Support ajax redirection.

parent 3acca93a
...@@ -163,12 +163,22 @@ yii = (function ($) { ...@@ -163,12 +163,22 @@ yii = (function ($) {
init: function () { init: function () {
var $document = $(document); var $document = $(document);
// automatically send CSRF token for all AJAX requests
$.ajaxPrefilter(function (options, originalOptions, xhr) { $.ajaxPrefilter(function (options, originalOptions, xhr) {
if (!options.crossDomain && pub.getCsrfVar()) { if (!options.crossDomain && pub.getCsrfVar()) {
xhr.setRequestHeader('X-CSRF-TOKEN', pub.getCsrfToken()); xhr.setRequestHeader('X-CSRF-Token', pub.getCsrfToken());
} }
}); });
// handle AJAX redirection
$document.ajaxComplete(function (event, xhr, settings) {
var url = xhr.getResponseHeader('X-Redirect');
if (url) {
window.location = url;
}
});
// handle data-confirm and data-method for clickable elements
$document.on('click.yii', pub.clickableSelector, function (event) { $document.on('click.yii', pub.clickableSelector, function (event) {
var $this = $(this); var $this = $(this);
if (pub.allowAction($this)) { if (pub.allowAction($this)) {
...@@ -178,6 +188,8 @@ yii = (function ($) { ...@@ -178,6 +188,8 @@ yii = (function ($) {
return false; return false;
} }
}); });
// handle data-confirm and data-method for changeable elements
$document.on('change.yii', pub.changeableSelector, function (event) { $document.on('change.yii', pub.changeableSelector, function (event) {
var $this = $(this); var $this = $(this);
if (pub.allowAction($this)) { if (pub.allowAction($this)) {
......
...@@ -126,8 +126,7 @@ class Controller extends \yii\base\Controller ...@@ -126,8 +126,7 @@ class Controller extends \yii\base\Controller
* Any relative URL will be converted into an absolute one by prepending it with the host info * Any relative URL will be converted into an absolute one by prepending it with the host info
* of the current request. * of the current request.
* *
* @param integer $statusCode the HTTP status code. If null, it will use 302 * @param integer $statusCode the HTTP status code. If null, it will use 302.
* for normal requests, and [[ajaxRedirectCode]] for AJAX requests.
* See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]] * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]]
* for details about HTTP status code * for details about HTTP status code
* @return Response the current response object * @return Response the current response object
......
...@@ -73,7 +73,7 @@ class Request extends \yii\base\Request ...@@ -73,7 +73,7 @@ class Request extends \yii\base\Request
/** /**
* The name of the HTTP header for sending CSRF token. * The name of the HTTP header for sending CSRF token.
*/ */
const CSRF_HEADER = 'X-CSRF-TOKEN'; const CSRF_HEADER = 'X-CSRF-Token';
/** /**
* @var boolean whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true. * @var boolean whether to enable CSRF (Cross-Site Request Forgery) validation. Defaults to true.
......
...@@ -112,13 +112,6 @@ class Response extends \yii\base\Response ...@@ -112,13 +112,6 @@ class Response extends \yii\base\Response
*/ */
public $charset; public $charset;
/** /**
* @var integer the HTTP status code that should be used when redirecting in AJAX mode.
* This is used by [[redirect()]]. A 2xx code should normally be used for this purpose
* so that the AJAX handler will treat the response as a success.
* @see redirect
*/
public $ajaxRedirectCode = 278;
/**
* @var string * @var string
*/ */
public $statusText; public $statusText;
...@@ -565,17 +558,22 @@ class Response extends \yii\base\Response ...@@ -565,17 +558,22 @@ class Response extends \yii\base\Response
/** /**
* Redirects the browser to the specified URL. * Redirects the browser to the specified URL.
*
* This method will send out a "Location" header to achieve the redirection. * This method will send out a "Location" header to achieve the redirection.
*
* In AJAX mode, this normally will not work as expected unless there are some * In AJAX mode, this normally will not work as expected unless there are some
* client-side JavaScript code handling the redirection. To help achieve this goal, * client-side JavaScript code handling the redirection. To help achieve this goal,
* this method will use [[ajaxRedirectCode]] as the HTTP status code when performing * this method will send out a "X-Redirect" header instead of "Location".
* redirection in AJAX mode. The following JavaScript code may be used on the client *
* side to handle the redirection response: * If you use the "yii" JavaScript module, it will handle the AJAX redirection as
* described above. Otherwise, you should write the following JavaScript code to
* handle the redirection:
* *
* ~~~ * ~~~
* $(document).ajaxSuccess(function(event, xhr, settings) { * $document.ajaxComplete(function (event, xhr, settings) {
* if (xhr.status == 278) { * var url = xhr.getResponseHeader('X-Redirect');
* window.location = xhr.getResponseHeader('Location'); * if (url) {
* window.location = url;
* } * }
* }); * });
* ~~~ * ~~~
...@@ -597,8 +595,7 @@ class Response extends \yii\base\Response ...@@ -597,8 +595,7 @@ class Response extends \yii\base\Response
* Any relative URL will be converted into an absolute one by prepending it with the host info * Any relative URL will be converted into an absolute one by prepending it with the host info
* of the current request. * of the current request.
* *
* @param integer $statusCode the HTTP status code. If null, it will use 302 * @param integer $statusCode the HTTP status code. If null, it will use 302.
* for normal requests, and [[ajaxRedirectCode]] for AJAX requests.
* See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]] * See [[http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html]]
* for details about HTTP status code * for details about HTTP status code
* @return Response the response object itself * @return Response the response object itself
...@@ -613,11 +610,14 @@ class Response extends \yii\base\Response ...@@ -613,11 +610,14 @@ class Response extends \yii\base\Response
if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) { if (strpos($url, '/') === 0 && strpos($url, '//') !== 0) {
$url = Yii::$app->getRequest()->getHostInfo() . $url; $url = Yii::$app->getRequest()->getHostInfo() . $url;
} }
if ($statusCode === null) {
$statusCode = Yii::$app->getRequest()->getIsAjax() ? $this->ajaxRedirectCode : 302; if (Yii::$app->getRequest()->getIsAjax()) {
$this->getHeaders()->set('X-Redirect', $url);
} else {
$this->getHeaders()->set('Location', $url);
} }
$this->getHeaders()->set('Location', $url);
$this->setStatusCode($statusCode); $this->setStatusCode($statusCode);
return $this; return $this;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment