问题
Im trying to make a webshop in Yii framework. Now i want to unset a session when a user clicks on a icon.
I currently have it working that is sends a json call to a file, but the url in the json call is being rewrited by my htaccess i think.
JSON call:
$(document).ready(function(){
$('.glyphicon-trash').click(function(){
$.getJSON("ajax/load.php?action=unset&element="+$(this).parent(), function(data) {
alert(data.message);
});
});
});
Error i get:
GET http://mydomain.nl/my/path/to/site/ajax/load 404 (Not Found)
But it's not load, its load.php! But my htaccess rewrites that url..
.htaccess
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
Other ways of implementing ajax calls in Yii are also good, only i need alot of explanation then.
回答1:
You can achieve these by core yii-ajax but if you want much work with ajax then you can use the core YII library with "renderPartial" ajax and widgets.
This is the core ajax call with yii
Add below line in your view.
<input type="hidden" id="baseUrl" value="<?php echo Yii::app()->getBaseUrl(true);?>">
Add below line in your js file.
function changeData(val1){
var baseUrl = $('#baseUrl').val();
var urlCI = baseUrl+'/Controller/actionMethod/;
$.ajax({
type: "GET",
url: urlCI,
data:{
'action':'unset',
'element' : 'someValue'
},
success: function(response) {
if(response!=''){
//do whatever work you want
return true;
}
else{
return false;
}
}
});
}
回答2:
Okay i got this working now, i made a file under site view. Now i call it like:
$(document).ready(function(){
$('.glyphicon-trash').click(function(){
var session = 'session';
$.getJSON("<?php echo Yii::app()->baseurl; ?>/load?action=unset&session="+session, function(data) {
location.reload(true); //<---- this one does not work tho...
});
setInterval(function(){location.reload(true);}, 500);
});
});
回答3:
You must not hardcode url's. You must use createUrl() or createAbsoluteUrl() as @Grimv01k recommends you. When in view or in controller you can do this by
$this->createUrl( 'foo/bar',array('id'=>321) )
Other ways:
Yii::app()->controller->createUrl('/baz/foo/bar'); // will use current controller
or
Yii::app()->createUrl('/baz/foo/bar')
Edited
So if you want
to unset a session when a user clicks on a icon
you can do this in following way.
echo CHtml::ajaxLink(
'click me to destroy session, mua-ha-ha',
Yii::app()->createUrl( '/session/destroy' ),
array(
'type' => 'post',
'dataType' => 'json',
'data' => array(
'foo' => 'bar',
),
'beforeSend' => 'js:function(){
console.log( $(this) );
return false;
}',
'success' => 'js:function(data){
console.log(data);
}',
)
);
in SessionController
:
public function actionDestroy () {
if ( isset( $_POST['foo'] ) ) {
Yii::app()->session->destroy();
if ( Yii::app()->request->isAjaxRequest ) {
$response = array(
'status' => !Yii::app()->session->sessionID
) ;
echo CJSON::encode( (object) $response );
Yii::app()->end();
}
}
}
来源:https://stackoverflow.com/questions/27185812/use-ajax-json-in-yii-framework