Page 1 of 1

Ajax Responce

Posted: Thu Nov 24, 2011 12:21 pm
by ChrisBull
Hi,Im using jquery to call a php file and then display the responce via alert. (in cakephp).
Here's my controller;

Code: Select all

public function login()
    {
        echo "hello";
    }
And my Jquery Code;

Code: Select all

function sendToPHP() 
{
    $.ajax({
        url:"http://www.testserver.com/accounts/login",
        type:"POST", 
        data:$('#loginForm').serialize(), 
        success:responseLogin, 
        context:this
    });
}
function responseLogin(response)
{
    alert(response);
}
So it should displa 'Hello'in the alert box but is is instead displaying the view for the 'login' action, which is the page we are already on.

Any Ideas?
Many Thanks
Chris

Re: Ajax Responce

Posted: Fri Nov 25, 2011 6:01 am
by maxx99
First, there is no need to url:"http://www.testserver.com/accounts/login" you won't be able to (and you shouldn't) do Cross-Site request (due to security reasons). So /accounts/login should be enough :)

Im not familiar with CakePHP but are you sure you're calling right url/action?
http://www.testserver.com/accounts/login should display only: "hello" is it the case?

Again im not familiar with CakePHP but if login is one of your actions there is a possibility that rest of the layout is sent with the response.
In Zend Framework e.g. you need to call

Code: Select all

  Zend_Layout::resetMvcInstance();
  $this->getResponse()->clearBody();
to display only hello for

Code: Select all

public function loginAction(){
echo "hello";
}