Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yii2
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PSDI Army
yii2
Commits
ae39324e
Commit
ae39324e
authored
Sep 20, 2013
by
Qiang Xue
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Support ajax redirection.
parent
3acca93a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
33 additions
and
22 deletions
+33
-22
yii.js
framework/yii/assets/yii.js
+13
-1
Controller.php
framework/yii/web/Controller.php
+1
-2
Request.php
framework/yii/web/Request.php
+1
-1
Response.php
framework/yii/web/Response.php
+18
-18
No files found.
framework/yii/assets/yii.js
View file @
ae39324e
...
@@ -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-T
OKEN
'
,
pub
.
getCsrfToken
());
xhr
.
setRequestHeader
(
'X-CSRF-T
oken
'
,
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
))
{
...
...
framework/yii/web/Controller.php
View file @
ae39324e
...
@@ -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
...
...
framework/yii/web/Request.php
View file @
ae39324e
...
@@ -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-T
OKEN
'
;
const
CSRF_HEADER
=
'X-CSRF-T
oken
'
;
/**
/**
* @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.
...
...
framework/yii/web/Response.php
View file @
ae39324e
...
@@ -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
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment