Hello! first at all, thanks for your attention!
I have a very weird problem, this is it: I have a Javascript file
(AJAX) that is sending a XMLHttpRequest via POST to a cakephp-
controller with has the Auth component implemented and a beforeFilter
function too; but when the request is made via AJAX the Auth component
in the controller doesn't handle the redirect to the loging page, thus
I can conclude that the Auth component doesn't work properly. This is
the code:
JAVASCRIPT:
<html>
<head>
<title>An Ajax demo</title>
<script language = "javascript">
var getVars = "_method=POST&data[Articulo][user_id]=1";
var XMLHttpRequestObject = new XMLHttpRequest();
function getData(){
if(XMLHttpRequestObject) {
XMLHttpRequestObject.open("POST", '/articulosXml/add/', true);
XMLHttpRequestObject.setRequestHeader("Content-Type",
"application/x-
www-form-urlencoded; charset=UTF-8");
XMLHttpRequestObject.setRequestHeader("X-Requested-With",
"XMLHttpRequest");
XMLHttpRequestObject.send(getVars);
}
}
</script>
</head>
<body>
<H1>An Ajax demo</H1>
<form>
<input type = "button" value = "Fetch the message"
onclick = "getData('targetDiv')">
</form>
</body>
</html>
CAKEPHP CONTROLER:
<?php
class ArticulosXmlController extends AppController {
var $name = 'ArticulosXml';
var $uses = array('Articulo');
var $autoRender = false;
var $components = array('Auth');
function beforeFilter() {
$this->Auth->loginAction = array
('controller'=>'users','action'=>'login');
$this->Auth->allow('index');
}
function index() {
Configure::write('debug', '0');
$this->Articulo->recursive = 0;
$this->set('articulos', $this->paginate());
$this->layout = 'ajax';
$this->render();
}
function add() {
$savedArtcl = false;
Configure::write('debug', '0');
if (!empty($this->data)) {
$this->layout = 'ajax';
$this->Articulo->create();
if ($this->Articulo->save($this->data)) {
$savedArtcl = true;
$this->set(compact('savedArtcl'));
$this->render();
} else {
$savedArtcl = false;
$this->set(compact('savedArtcl'));
$this->render();
}
}
}
}
?>
WEIRD NOTES:
- if I access via http to http://localhost/articulosXml/add the Auth
component works fine, it makes the redirect to http://localhost/users/login
(OK)
- if I access via http to http://localhost/articulosXml/index the Auth
component woks fine, it makes the redirect to http://localhost/users/login
(OK)
- if I access via XMLHttpRequest to /articulosXml/add, the Auth
component doesn't work (WRONG)
My question again is: why the Auth component and the beforeFilter()
function doesn't work when I use a XMLHttpRequest request???. Note
that if you change POST to GET in the javascript the Auth component
works. Maybe is a BUG... who knows!!!
XMLHttpRequestObject.open("GET", '/articulosXml/add/', true); // <-
WORKS FINE
XMLHttpRequestObject.open("POST", '/articulosXml/add/', true); // <-
DO NOT WORK
Does anyone have a clue about what is happening?
Thanks for your attention!
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"CakePHP" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---